Skip to content

Lesson 5 of 14 · 18m

NULL Means Unknown: Three-Valued Logic

= NULL silently returns nothing, NOT IN with one NULL empties the whole set, and COUNT(col) skips NULLs by design.

WHERE deleted_at = NULL returns zero rows. Every time, no matter how many soft-deleted rows exist, no error, no warning. This is the single most common silent-bug generator in SQL, and it happens because NULL is not a value — it's the absence of one.

Postgres evaluates every predicate to one of three outcomes: true, false, or unknown. WHERE keeps only rows where the predicate is true. Any comparison involving NULL — =, <>, <, >, IN — evaluates to NULL (unknown), and unknown is not true, so the row is dropped. deleted_at = NULL is unknown for every row in the table, so the result is empty. The correct test is deleted_at IS NULL.

Three-valued logic produces one genuinely nasty production bug. NOT IN with a NULL in the list empties the entire set:

DELETE FROM users
WHERE id NOT IN (SELECT user_id FROM orders);

If orders.user_id contains a single NULL, this deletes zero rows. Unroll why: x NOT IN (1, 2, NULL) means x <> 1 AND x <> 2 AND x <> NULL. The last term is unknown, AND with unknown yields unknown, and unknown is not true — every row is filtered out. Nobody inserts a NULL user_id on purpose; a bug in one writer does it, and your cleanup query silently does nothing for months until someone checks. The fix is NOT EXISTS, which treats the subquery as a semi-join row-by-row and is immune:

DELETE FROM users u
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);

Aggregates have their own NULL rules. COUNT(*) counts rows; COUNT(col) counts rows where col is not NULL; COUNT(DISTINCT col) counts distinct non-NULL values. So COUNT(*) - COUNT(phone) is the number of NULL phone numbers — a useful audit trick. SUM, AVG, MIN, MAX all skip NULLs, which means AVG(response_ms) reports only the requests that recorded a value — whether that's what you meant is your problem.

Uniqueness treats NULLs as incomparable: a UNIQUE (email) index happily stores fifty rows with email IS NULL, because NULL can't equal NULL and therefore can't violate uniqueness. If "absent" should be unique too, Postgres 15 added NULLS NOT DISTINCT (UNIQUE NULLS NOT DISTINCT (email)); on older versions the workaround is a partial unique index WHERE email IS NULL on a generated marker column or — better — not storing NULLs.

Two tools earn their place in the muscle memory: COALESCE(a, b, c) returns the first non-NULL argument — COALESCE(nickname, email) for display fallbacks, COALESCE(balance, 0) for arithmetic that must not go unknown — and IS DISTINCT FROM is the null-safe equality operator: a IS DISTINCT FROM b is true when exactly one side is NULL or they differ, which is the comparison you actually want in change-detection and reconciliation queries. ORDER BY puts NULLs last on ASC (they sort "larger than everything") and first on DESC; NULLS FIRST/NULLS LAST overrides.

And the constraint interaction from earlier: a CHECK rejects only false. CHECK (qty > 0) passes qty = NULL because NULL > 0 is unknown, and unknown is not false. NOT NULL and CHECK are a pair, not alternatives.

Try this: create a users table and an orders table, insert one orders row with user_id = NULL, then run the NOT IN delete and the NOT EXISTS delete. Compare row counts. Then SELECT COUNT(*), COUNT(user_id) FROM orders and explain the difference out loud.

Pitfalls

  • NULL = NULL is unknown even between two NULLs — you can't join on "both are NULL" without IS NOT DISTINCT FROM.
  • IN/NOT IN are only safe when the column or subquery is provably non-NULL. If you can't prove it, write EXISTS/NOT EXISTS and stop thinking about it.
  • Application "or" filters — WHERE (email = $1 OR email IS NULL) style hacks to emulate null-safe equality — are a sign you wanted IS NOT DISTINCT FROM.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.