Re: Multiple natural joins - Mailing list pgsql-general

From Peter Eisentraut
Subject Re: Multiple natural joins
Date
Msg-id 49C25540.9020606@gmx.net
Whole thread Raw
In response to Multiple natural joins  (Thom Brown <thombrown@gmail.com>)
Responses Re: Multiple natural joins  (Sam Mason <sam@samason.me.uk>)
List pgsql-general
Thom Brown wrote:
> SELECT tbl_a.location, tbl_b.language
> FROM tbl_c
> NATURAL INNER JOIN tbl_a
> NATURAL INNER JOIN tbl_b
>
> The confusion comes when 2 of those tables reference the 3rd table using
> the same column.
>
> So are natural joins only allowed to join 2 tables?  If not, how can it
> be used for more than 1 table has links to the other tables?

The fact that the above example runs proves that natural joins are
allowed with more than 2 tables.

Joins nest from left to write, so

tbl_c NATURAL JOIN tbl_a NATURAL JOIN tbl_b

means

(tbl_c NATURAL JOIN tbl_a) NATURAL JOIN tbl_b

and that means that the second join is executed as if the first join
resulted in a normal table with normal columns with names as the case
may be, and the natural join will use those names with all the consequences.

If you want a different order, you can set the parentheses differently,
with possibly different results.

The fact that this isn't entirely obvious only supports the argument
that natural joins shouldn't used.


pgsql-general by date:

Previous
From: Milos Findura
Date:
Subject: Re: deadlock problem
Next
From: "Rob Richardson"
Date:
Subject: Changing pg_type records