Received: from sss.pgh.pa.us (sss.pgh.pa.us [209.114.132.154]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f21Iu9x75536 for ; Thu, 1 Mar 2001 13:56:10 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.1/8.11.1) with ESMTP id f21Iu6921960; Thu, 1 Mar 2001 13:56:06 -0500 (EST) To: Frank Joerdens cc: pgsql-sql@postgresql.org Subject: Re: Weird NOT IN effect with NULL values In-reply-to: <20010301191654.A12406@rakete.joerdens.de> References: <20010301191654.A12406@rakete.joerdens.de> Comments: In-reply-to Frank Joerdens message dated "Thu, 01 Mar 2001 19:16:54 +0100" Date: Thu, 01 Mar 2001 13:56:06 -0500 Message-ID: <21957.983472966@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200103/13 X-Sequence-Number: 1655 Frank Joerdens 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 is true for at least one row RT in T, then "R T" is true. d) If T is empty or if the implied is false for every row RT in T, then "R T" is false. e) If "R 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