Re: left joins

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Grant Morgan" <grant(at)ryuuguu(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: left joins
Date: 2005-07-06 14:44:14
Message-ID: 26225.1120661054@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Grant Morgan" <grant(at)ryuuguu(dot)com> writes:
> select count(*)
> from h left join p using (r,pos)
> where h.tn > 20
> and h.tn < 30
> and p.r_order=1

> since it is a left join I though I should get a number no smaller in
> the left join than the original unjoined query. It seems to be acting
> like an inner join.

Well, yeah. The condition p.r_order=1 will return NULL (effectively
FALSE) for any row in which p.r_order is NULL, so none of the
null-extended rows can survive the WHERE filter, so it's effectively
an inner join. Recent versions of PG actively recognize this case
and reduce the LEFT JOIN to plain JOIN, but even if we did not do that
you'd get the same result.

I've heard it claimed that Oracle produces different results; if true,
it must have something to do with their rather standards-challenged
interpretation of NULL ...

regards, tom lane

In response to

  • left joins at 2005-07-06 10:01:54 from Grant Morgan

Browse pgsql-sql by date

  From Date Subject
Next Message Jocelyn Turcotte 2005-07-06 19:32:26 Prepare plan in plpgsql
Previous Message Grant Morgan 2005-07-06 10:52:11 Re: left joins