Three-valued logic in practice
SQL has three truth values: TRUE, FALSE, and NULL ('unknown'). Any operation involving NULL evaluates to NULL — including NULL = NULL. WHERE only returns rows where the condition is TRUE, so NULLs silently drop out unless you handle them explicitly.
The traps
WHERE x = NULLnever matches. UseIS NULL.WHERE x <> 'something'excludes rows where x is NULL. AddOR x IS NULLif you want them.NOT IN (subquery)returns zero rows if the subquery contains any NULL. UseNOT EXISTS.- Aggregates (SUM, AVG, COUNT(col)) silently skip NULL.
Tools to handle NULL deliberately
COALESCE(a, b, c)— first non-NULL value.NULLIF(a, b)— NULL if a equals b (great for avoiding division by zero).IS DISTINCT FROM/IS NOT DISTINCT FROM— NULL-safe equality.