X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 13922310406 for ; Sat, 28 Jun 2003 15:43:59 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 29106-03 for ; Sat, 28 Jun 2003 12:43:48 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 480BA30FC60 for ; Sat, 28 Jun 2003 12:43:48 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h5SFhlQQ018086; Sat, 28 Jun 2003 11:43:47 -0400 (EDT) To: Ludwig Lim Cc: PostgreSQL Mailing List Subject: Re: Question on OUTER JOINS. In-reply-to: <20030628061618.19440.qmail@web80510.mail.yahoo.com> References: <20030628061618.19440.qmail@web80510.mail.yahoo.com> Comments: In-reply-to Ludwig Lim message dated "Fri, 27 Jun 2003 23:16:18 -0700" Date: Sat, 28 Jun 2003 11:43:47 -0400 Message-ID: <18085.1056815027@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200306/329 X-Sequence-Number: 14087 Ludwig Lim 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