Subject: INNER JOINS in sql-select.html

From: Stefan Weiss <[email-masked]>
Date: Mon, 27 Oct 2003 23:09:37 +0100

Hi.

>From <doc/html/sql-select.html>:
| A CROSS JOIN or INNER JOIN is a simple Cartesian product, the same
| as you get from listing the two items at the top level of FROM. CROSS
| JOIN is equivalent to INNER JOIN ON (TRUE), that is, no rows are
| removed by qualification. These join types are just a notational
| convenience, since they do nothing you couldn't do with plain FROM 
| and WHERE.

Is there really no difference between these two queries?

SELECT blarg
  FROM ta, tb, tc, [...]
 WHERE ta.foo = tb.bar
   AND tb.bar = tc.baz
   AND [...]

SELECT blarg
  FROM ta
  JOIN tb ON tb.bar = ta.foo
  JOIN tc ON tc.baz = tb.bar
  JOIN [...]

I thought that by using the second form, you would be able to do
'explicit' joins, effectivly telling the planner in which order to
join multiple tables (in case you have to join 10+ tables)?

cheers,
stefan

---

From: Tom Lane <[email-masked]>
Date: Thu, 30 Oct 2003 17:56:04 -0500

Stefan Weiss <[email-masked]> writes:
> | removed by qualification.

> join multiple tables (in case you have to join 10+ tables)?

They are semantically equivalent, but not necessarily the same from a
performance point of view.  The potential performance issues are covered
elsewhere; I think it would just obfuscate matters to try to include
that topic here.

			regards, tom lane

---

From: "Henry B. Hotz" <[email-masked]>
Date: Mon, 3 Nov 2003 15:08:05 -0800

You can imply the issue without obfuscating things.  How about:

>A CROSS JOIN or INNER JOIN is a simple Cartesian product, the same 
>as you get from listing the two items at the top level of FROM. 
>CROSS JOIN yields the same results as INNER JOIN ON (TRUE), that is, 
>no rows are removed by qualification.

-- 
The opinions expressed in this message are mine,
not those of Caltech, JPL, NASA, or the US Government.
[email-masked], or [email-masked]

---

From: Tom Lane <[email-masked]>
Date: Mon, 03 Nov 2003 19:38:21 -0500

> no rows are removed by qualification.

Okay, but that doesn't do the trick --- it implies that CROSS JOIN isn't
equivalent to INNER JOIN ON (TRUE), when in fact they are equivalent,
both as to result and performance characteristics.  The issue at hand is
that an explicit "a JOIN b" may not be equivalent to "FROM a, b".

I reworded the passage as

        CROSS JOIN and INNER JOIN
        produce a simple Cartesian product, the same result as you get from
        listing the two items at the top level of FROM,
	but restricted by the join condition (if any).
        CROSS JOIN is equivalent to INNER JOIN ON
        (TRUE), that is, no rows are removed by qualification.

does that help?

			regards, tom lane

---

From: "Henry B. Hotz" <[email-masked]>
Date: Mon, 3 Nov 2003 17:35:11 -0800

>         (TRUE), that is, no rows are removed by qualification.

'sarright.  I was just wordsmithing without worrying about the meaning.
-- 
The opinions expressed in this message are mine,
not those of Caltech, JPL, NASA, or the US Government.
[email-masked], or [email-masked]