Difference between IN and JOIN

Difference between IN and JOIN

am 04.10.2004 10:39:50 von twanger

Hi,

I lately wondered if there is a difference between a JOIN and a IN in
queries similar to the following:

SELECT f1 FROM t1 JOIN t2 ON (t.f2 = t2.f2) WHERE t2.f3 = x

SELECT f1 FROM t1 WHERE t1.f2 IN (SELECT f2 FROM t2 WHERE f3 = x)

As I see it there's no semantic difference between the two. Are there
differences in performance or anything else that matters? The execution
plans seem to match except for the use of an "Hash IN Join" in place of
a "Hash Join". Estimated costs match.

Thanks for your advice

--
Markus Bertheau


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Re: Difference between IN and JOIN

am 04.10.2004 16:17:16 von tgl

Markus Bertheau writes:
> I lately wondered if there is a difference between a JOIN and a IN in
> queries similar to the following:

> SELECT f1 FROM t1 JOIN t2 ON (t.f2 = t2.f2) WHERE t2.f3 = x

> SELECT f1 FROM t1 WHERE t1.f2 IN (SELECT f2 FROM t2 WHERE f3 = x)

> As I see it there's no semantic difference between the two.

There's plenty of difference, if t2 contains repeated occurrences of f2
values.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Re: Difference between IN and JOIN

am 04.10.2004 16:23:49 von twanger

В Пнд, 04.10.2004, в 16:17, Tom Lane пи=
шет:
> Markus Bertheau writes:
> > I lately wondered if there is a difference between a JOIN and a IN in
> > queries similar to the following:
>=20
> > SELECT f1 FROM t1 JOIN t2 ON (t.f2 =3D t2.f2) WHERE t2.f3 =3D x
>=20
> > SELECT f1 FROM t1 WHERE t1.f2 IN (SELECT f2 FROM t2 WHERE f3 =3D x)
>=20
> > As I see it there's no semantic difference between the two.
>=20
> There's plenty of difference, if t2 contains repeated occurrences of f2
> values.

In my case f2 is the primary key of t2. I probably didn't state the
point of my question well enough: You can write an IN query as a
semantically equal JOIN. I wondered, which one I should prefer, and on
which grounds.

Thanks

--=20
Markus Bertheau


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly