Re: exists and is not null equivalence in query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Raphael Bauduin <rblists(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: exists and is not null equivalence in query
Date: 2009-05-28 15:19:04
Message-ID: 9371.1243523944@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Raphael Bauduin <rblists(at)gmail(dot)com> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jamie Tufnell 2009-06-01 03:54:52 Assigning data-entry tasks to multiple concurrent clients
Previous Message Raphael Bauduin 2009-05-28 12:36:31 exists and is not null equivalence in query