Re: Complex outer joins? - Mailing list pgsql-sql

From Stephan Szabo
Subject Re: Complex outer joins?
Date
Msg-id 20030326074807.H61005-100000@megazone23.bigpanda.com
Whole thread Raw
In response to Re: Complex outer joins?  (Greg Stark <gsstark@mit.edu>)
Responses Re: Complex outer joins?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
On 26 Mar 2003, Greg Stark wrote:

> Tom Lane <tgl@sss.pgh.pa.us> writes:
>
> > The SQL-standard way of writing this would presumably be either
> >
> >     from G left join L on (G.SELID = L.SELID)
> >          left join C on (L.SELID = C.SELID and L.SELLEVEL = C.SELLEVEL)
>
> I would think of it as this one.
>
> >     from G left join
> >          (L left join C on (L.SELID = C.SELID and L.SELLEVEL = C.SELLEVEL))
> >          on (G.SELID = L.SELID)
>
> I don't see how that would be at all different.
>
> > depending on which join you think ought to be done first.  It might be
> > that the results are the same in this case, but I'm not convinced of
> > that.  In general the results of outer joins definitely depend on join
> > order.
>
> I'm pretty sure Oracle actually builds an abstract join representation where
> the two queries above would actually be represented the same way. Then decides
> the order from amongst the equivalent choices based on performance decisions.
>
> Can you show an example where the join order would affect the result set? I
> can't think of any.

I can think of a few somewhat degenerate cases.  I believe if you add an
(or l.sellevel is null) to the second join's on clause.  In the first if
there's no match between g and l then sellevel is null and you'll join
with all rows of c.  In the second, you'll do that join first (and
therefore only join all the rows with ones where the column really is
null) and then join with g, and if there's no match, you'll get one row
with nulls for the l and c columns.



pgsql-sql by date:

Previous
From: Greg Stark
Date:
Subject: Re: Complex outer joins?
Next
From: Josh Berkus
Date:
Subject: Re: Does anyone use TO_CHAR(INTERVAL)?