| From: | "Jean-Pierre Pelletier" <pelletier_32(at)sympatico(dot)ca> | 
|---|---|
| To: | "Daniel Afonso Heisler" <daniel(at)solis(dot)coop(dot)br> | 
| Cc: | <pgsql-bugs(at)postgresql(dot)org> | 
| Subject: | Re: BUG #2178: NOT IN command don't work | 
| Date: | 2006-01-24 16:56:45 | 
| Message-ID: | BAYC1-PASMTP0442C4EBB587635B8521F195130@CEZ.ICE | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-bugs | 
The expected behavior can be obtained by filtering out the null in
the subquery or by using "not exists" instead of "not in".
Here is an example:
CREATE TEMPORARY TABLE subquerytable (column1 INTEGER);
INSERT INTO subquerytable VALUES(2);
INSERT INTO subquerytable VALUES(NULL);
INSERT INTO subquerytable VALUES(3);
SELECT true WHERE 1 NOT IN (SELECT column1 FROM subquerytable); -- Wrong
SELECT true WHERE 1 NOT IN (SELECT column1 FROM subquerytable WHERE column1 
IS NOT NULL); -- Ok
SELECT true WHERE NOT EXISTS(SELECT * FROM subquerytable WHERE 1 = 
column1); -- Ok
It's not clear to me why "not exists" and "not in" return a different result 
but it must be per SQL spec
as all DBMS I have seen do that (Oracle, SQL Server, MYSQL, ...)
In most queries I have seen column1 is NOT NULL so IN or EXISTS can both be 
used safely.
Jean-Pierre Pelletier
e-djuster 
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Alvaro Herrera | 2006-01-24 22:21:10 | Re: BUG #2204: Feature Req: Unique output column names | 
| Previous Message | Tom Lane | 2006-01-24 16:44:03 | Re: BUG #2204: Feature Req: Unique output column names |