X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id D531F475CBC for ; Wed, 26 Mar 2003 10:52:32 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id CB5C4474E44 for ; Wed, 26 Mar 2003 10:52:28 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 403B5D5FA; Wed, 26 Mar 2003 07:52:33 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 35F465C0B; Wed, 26 Mar 2003 07:52:33 -0800 (PST) Date: Wed, 26 Mar 2003 07:52:33 -0800 (PST) From: Stephan Szabo To: Greg Stark Cc: Tom Lane , "Correia, Carla" , "'pgsql-sql@postgresql.org'" Subject: Re: Complex outer joins? In-Reply-To: <87smtakxes.fsf@stark.dyndns.tv> Message-ID: <20030326074807.H61005-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-25.8 required=5.0 tests=BAYES_00,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/582 X-Sequence-Number: 12620 On 26 Mar 2003, Greg Stark wrote: > Tom Lane 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.