Re: I don't understand... - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: I don't understand...
Date
Msg-id Pine.LNX.4.30.0105221637010.757-100000@peter.localdomain
Whole thread Raw
In response to I don't understand...  ("Gabor Csuri" <gcsuri@coder.hu>)
List pgsql-hackers
Gabor Csuri writes:

> SELECT DISTINCT h_name
> >FROM haszon
> WHERE h_name NOT IN (SELECT cn_name FROM carname)
>
> +--------+
> | h_name |
> +--------+
> +--------+
> Query OK, 0 rows in set (0,10 sec)
>
> Why ?

Because one of the cn_name values is NULL.  Observe the semantics of the
IN operator if the set contains a NULL value:

h_name NOT IN (a, b, c)
NOT (h_name = a OR h_name = b OR h_name = c)

Say c is null:

NOT (h_name = a OR h_name = b OR h_name = NULL)
NOT (h_name = a OR h_name = b OR NULL)
NOT (NULL)
NULL

which is false.

You might want to add a ... WHERE cn_name IS NOT NULL in the subquery.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



pgsql-hackers by date:

Previous
From: "Sergey E. Volkov"
Date:
Subject: Re: PL/pgSQL CURSOR support
Next
From: "Ross J. Reedstrom"
Date:
Subject: Re: Re: I don't understand...