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 = NULLis unknown even between two NULLs — you can't join on "both are NULL" withoutIS NOT DISTINCT FROM.IN/NOT INare only safe when the column or subquery is provably non-NULL. If you can't prove it, writeEXISTS/NOT EXISTSand 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 wantedIS NOT DISTINCT FROM.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
MySQL, PostgreSQL, Redis, and MongoDB End-of-Life Dates in 2026
Four major open source databases are hitting end-of-life milestones in 2026, and MySQL 8.0 has already crossed that line. Here's a practical, code-first look at how to audit what you're running, back it up properly, and migrate before the risk compounds.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
PostgreSQL Fixed 52 CVEs in One Release. The Upgrade Is the Easy Part.
PostgreSQL 18.7, 17.12, 16.16, 15.20 and 14.25 fix 52 security vulnerabilities, almost twice August's record of 28. The binary swap took under a second in my test. The post-update steps are where the work is, and one of them makes some indexes several times bigger.
What PostgreSQL 15 Changed
A new version of the popular open-source database PostgreSQL is now available, offering performance improvements over PostgreSQL 14 and new functions and capabilities for managing workloads in local and distributed deployments. Released on October 13 by the PostgreSQL Global Development Group,…