I had a query (for Postgres and Informix) with a NOT IN clause containing a subquery that in some cases returned NULL values, causing that clause (and the entire query) to fail to return anything.
What's the best way to understand this? I thought of NULL as something without a value, and therefore wasn't expecting the query to fail, but obviously that's not the correct way to think of NULL.
x <> NULLas resolving toFALSE, you would expectNOT (x <> NULL)to evaluate toTRUE, and it doesn't. Both evaluate toUNKNOWN. The trick is that a row is selected only if theWHEREclause (if present) evaluates toTRUE-- a row is omitted if the clause evaluates to eitherFALSEorUNKNOWN. This behavior (in general, and for theNOT INpredicate in particular) is mandated by the SQL standard. – kgrittn May 03 '12 at 14:35NULL NOT IN (some_subquery)should not return the outer row except ifsome_subquerydoesn't return any rows. Which is why the execution plan when both columns are Null-able can be considerably more expensive. SQL Server Example – Martin Smith Jun 18 '12 at 07:08