Re: Question on OUTER JOINS.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>
Cc: PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Question on OUTER JOINS.
Date: 2003-06-28 15:43:47
Message-ID: 18085.1056815027@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com> writes:
> Is there way of rewritting :

> SELECT a.status,
> employee_id
> FROM permission a LEFT JOIN
> ( SELECT * FROM employee WHERE employee_id
> =5) as b ON (a.status = b.status)
> WHERE status='test'

> into a query that has no subselect in the FROM clause.

In this particular case you could do

FROM permission a LEFT JOIN
employee b ON (a.status = b.status AND b.employee_id=5)
WHERE status='test'

which AFAICS would give the same answers. As Bruno points out,
you can't move qualification conditions up and down past outer
joins without changing the answers in general. But the above
change is okay: either way, A rows that don't match to a B row
with employee_id=5 will be emitted with nulls instead.

However, in this particular case I don't see why you're bothering.
If the sub-SELECT can be flattened, the planner will generally do
it for you. I'd expect the first form of the query to give the
same plan (in pre-7.4 releases, maybe even a better plan) as the
second.

Perhaps you're showing us an oversimplified version of your
real problem?

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tomasz Myrta 2003-06-30 07:46:03 Re: Postgres - Delphi Application
Previous Message Bruno Wolff III 2003-06-28 11:22:41 Re: Question on OUTER JOINS.