pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Frank Joerdens <frank@joerdens.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: Weird NOT IN effect with NULL values
Date: Thu, 01 Mar 2001 13:56:06 -0500
Message-ID: <21957.983472966@sss.pgh.pa.us> (raw)
In-Reply-To: <20010301191654.A12406@rakete.joerdens.de>
References: <20010301191654.A12406@rakete.joerdens.de>

Frank Joerdens <frank@joerdens.de> writes:
> When doing a subselect with NOT IN, as in
> SELECT name
> FROM customer
> WHERE customer_id NOT IN (
> SELECT customer_id
> FROM salesorder
> );
> I get no rows if the result column returned by the subselect
> contains NULL values. It works as expected if I remove the NULL values
> from the result set. Is this behaviour correct and if so, why?

This is correct because of SQL's 3-valued boolean logic.  SQL92 defines
"A NOT IN B" as equivalent to "NOT (A = SOME B)", and the latter has
the rules

            c) If the implied <comparison predicate> is true for at least
              one row RT in T, then "R <comp op> <some> T" is true.

            d) If T is empty or if the implied <comparison predicate> is
              false for every row RT in T, then "R <comp op> <some> T" is
              false.

            e) If "R <comp op> <quantifier> T" is neither true nor false,
              then it is unknown.

Now the implied comparison will generate TRUE for the subselect rows
that contain a matching customer_id, FALSE for the rows that contain
non-matching (but not null) customer_id, and UNKNOWN (null) for the
rows that contain nulls.  So if you have nulls then case (d) never
holds: the result of A = SOME B is either true or unknown.  And so
the result of NOT IN is either false or unknown, and either way the
outer WHERE fails.

This can be justified intuitively if you consider that null means
"don't know": you can say for sure that the target customer_id IS in
the subselect if you find it there, but you can't say for sure that it
IS NOT there, because you don't know all the subselect result elements.

Bottom line: you probably want to suppress nulls in the subselect...

			regards, tom lane



view thread (8+ messages)  latest in thread

Message-ID: <21957.983472966@sss.pgh.pa.us>
Permalink:  ../21957.983472966@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21957.983472966@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, frank@joerdens.de
  Subject: Re: Weird NOT IN effect with NULL values
  In-Reply-To: <21957.983472966@sss.pgh.pa.us>
  References: <20010301191654.A12406@rakete.joerdens.de> <21957.983472966@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox