pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tara Piorkowski <tara@vilaj.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: RIGHT JOIN Table Ordering Question
Date: Sun, 30 Dec 2001 17:15:25 -0500
Message-ID: <1525.1009750525@sss.pgh.pa.us> (raw)
In-Reply-To: <3C2F630B.5060500@vilaj.com>
References: <3C2F630B.5060500@vilaj.com>

Tara Piorkowski <tara@vilaj.com> writes:
> I have a question about the ordering of joining of tables using the 
> RIGHT JOIN syntax in the FROM clause of a query. Specifically, I've 
> noticed that if I have one table on which I attempt to right join to two 
> other tables, the ordering of the join conditions is important

Yup, it sure is.  Outer joins are not associative (nor commutative,
obviously).  When you write (a RIGHT JOIN b) RIGHT JOIN c, you first
have the result of the A/B join, which will be guaranteed to produce all
the joined rows an inner join would produce, plus a row with null A
values for each otherwise unmatched B row.  Then you take this and join
it to C, again adding rows for each unmatched C row.  This is not
associative because what's matched or not in the second step may depend
on what got added in the first step.  In your example, try looking at
the whole join results without any WHERE filter:

regression=# select * from goal_progress_notes a
regression-# RIGHT JOIN goals c USING (goal_id);
 goal_id | goal_prog_note_id | rating_id |      statement      |               statement
---------+-------------------+-----------+---------------------+----------------------------------------
       1 |                 1 |         1 | Tara is doing well. | Will write a correct SQL92 LEFT JOIN.
       2 |                   |           |                     | Will write a correct SQL92 RIGHT JOIN.
(2 rows)

regression=# select * from goal_progress_notes a
regression-# RIGHT JOIN goals c USING (goal_id)
regression-# RIGHT JOIN progress_ratings b USING (rating_id);
 rating_id | goal_id | goal_prog_note_id |      statement      |               statement               | description
-----------+---------+-------------------+---------------------+---------------------------------------+--------------
         1 |       1 |                 1 | Tara is doing well. | Will write a correct SQL92 LEFT JOIN. | Achieved
         2 |         |                   |                     |                                       | Not Achieved
(2 rows)

I would imagine that what you really want in this example is
	(goals c LEFT JOIN goal_progress_notes a USING (goal_id))	
	LEFT JOIN progress_ratings b USING (rating_id)
since you definitely want an output row for every goals row whether
there are notes or not, and you don't really want output rows for
progress ratings that happen to not be used currently (do you)?
Depending on your viewpoint about goal progress notes that don't
match any goal, perhaps the first join should be a FULL join.

> understanding fully the RIGHT JOIN/LEFT JOIN sequence (I originally come 
> from an Oracle SQL background, which is different)

AFAIK Oracle has the same semantics for left/right joins, just an
obscure syntax.

			regards, tom lane



view thread (2+ messages)

Message-ID: <1525.1009750525@sss.pgh.pa.us>
Permalink:  ../1525.1009750525@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1525.1009750525@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tara@vilaj.com
  Subject: Re: RIGHT JOIN Table Ordering Question
  In-Reply-To: <1525.1009750525@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox