Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4449D632D2C for ; Thu, 28 May 2009 12:20:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 33981-10 for ; Thu, 28 May 2009 12:20:48 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 5444D63357F for ; Thu, 28 May 2009 12:19:06 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id n4SFJ4xr009372; Thu, 28 May 2009 11:19:04 -0400 (EDT) To: Raphael Bauduin cc: pgsql-sql@postgresql.org Subject: Re: exists and is not null equivalence in query In-reply-to: References: Comments: In-reply-to Raphael Bauduin message dated "Thu, 28 May 2009 14:36:31 +0200" Date: Thu, 28 May 2009 11:19:04 -0400 Message-ID: <9371.1243523944@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/113 X-Sequence-Number: 32605 Raphael Bauduin writes: > select count(t.trame_id) as count, v.voiture_num as voitureNum from > arch_trames t left join voiture v on (v.tag_id=t.tag_id) > where > (t.recept_time >= 1243509320691) > and exists (select v2.voiture_num from voiture v2 where v2.tag_id=v.tag_id) > group by v.voiture_num order by v.voiture_num > Am I right that I can replace the "and exists..." clause by > "and v.voiture_num is not null " in this case? Well, more like "and v.tag_id is not null". But yes, the EXISTS seems pretty stupid. Also, having done that, the left join reduces to a plain inner join and you don't even need the NOT NULL. IOW this should just be select count(t.trame_id) as count, v.voiture_num as voitureNum from arch_trames t join voiture v on (v.tag_id=t.tag_id) where (t.recept_time >= 1243509320691) group by v.voiture_num order by v.voiture_num regards, tom lane