Thread: BUG #4422: select ... where ... NOT EXISTS / NOT IN

BUG #4422: select ... where ... NOT EXISTS / NOT IN

From
"vasile"
Date:
The following bug has been logged online:

Bug reference:      4422
Logged by:          vasile
Email address:      vasile@gmail.com
PostgreSQL version: 8.2.4
Operating system:   Centos 4
Description:        select ... where ...  NOT EXISTS / NOT IN
Details:

I have this 2 similar queries.

Why the 1st query is returning 1000+ rows and the 2nd one no rows ?
  The col1 is not empty in both tables.

1)
SELECT col1, col2, col3
FROM table1 t1
WHERE NOT EXISTS (SELECT t2.col1 FROM table2 t2 WHERE t1.col1 = t2.col1 );

2)
SELECT col1, col2, col3
FROM table1 t1
WHERE t1.col1 NOT IN (SELECT t2.col1 FROM table2 t2 );



If I build the query with "LEFT JOIN" I have the same result set like in the
1st query:

SELECT t1.col1, t1.col2, t1.col3
FROM table1 t1
LEFT JOIN table2 t2 ON (t1.col1 = t2.col1)
WHERE t2.col1 is null;

Re: BUG #4422: select ... where ... NOT EXISTS / NOT IN

From
hubert depesz lubaczewski
Date:
On Wed, Sep 17, 2008 at 02:53:51PM +0000, vasile wrote:
> I have this 2 similar queries.
>
> Why the 1st query is returning 1000+ rows and the 2nd one no rows ?
>   The col1 is not empty in both tables.

there is no bug.
check this:

http://www.depesz.com/index.php/2008/08/13/nulls-vs-not-in/

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007