0

I have 2 table foo2 and foo3 with the pictures below. These tables have unique column -> id.

table foo2:

enter image description here

table foo3:

enter image description here

I want to select data from two tables with same id. I have 2 query like this

query1:

SELECT
  foo2.id,
  foo2.`name`
FROM
  foo2
WHERE foo2.id IN (SELECT id FROM foo3);

query2:

SELECT
    foo2.id,
    foo2.`name`
FROM
    foo2
INNER JOIN foo3 ON (foo2.id = foo3.id);

These queries return same data. I want to know which one is better? Which one is efficient?

Thanks for answers.

Kemal Güler
  • 608
  • 1
  • 6
  • 21

2 Answers2

1

If you want to know which performs better, then test the two queries on your data and on your system.

It is important to note that the two queries are not the same. They may return the same value on your data, but the JOIN could return duplicate rows, if there are duplicates in foo3.

As a personal preference, I would tend to use JOIN rather than IN if the two are going to return the same rows (say, because I know that foo3.id is unique).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You can go with Query2 approach.

As mention by you in second approach INNER JOIN may good in scenario where table foo3 had millions of rows. So where clause may took more time then INNER JOIN.

Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38