SELECT count(*) FROM users; -- 1000
SELECT count(*) FROM users WHERE country = 'DE'; -- 300
SELECT count(*) FROM users WHERE country <> 'DE'; -- 550 ?!
300 + 550 isn't 1,000. The missing 150 rows have country = NULL, and they're in neither result. NULL isn't a value in SQL. It means "unknown", and comparisons with unknown aren't true or false. They're unknown too.
Three-valued logic
Every comparison in SQL returns TRUE, FALSE or UNKNOWN:
NULL = 'DE' -- UNKNOWN
NULL <> 'DE' -- UNKNOWN
NULL = NULL -- UNKNOWN (not TRUE!)
WHERE, ON, HAVING and CHECK-style filters keep a row only when the condition is TRUE. UNKNOWN rows are dropped, so NULLs silently disappear from both country = 'DE' and country <> 'DE'.
Fix: say what you mean about NULLs
-- "not Germany, including unknown"
WHERE country <> 'DE' OR country IS NULL
-- null-safe inequality
WHERE country IS DISTINCT FROM 'DE' -- PostgreSQL, SQL Server 2022+, SQLite ("IS NOT")
WHERE NOT (country <=> 'DE') -- MySQL / MariaDB null-safe equality
IS DISTINCT FROM treats NULL as an ordinary comparable value: NULL IS DISTINCT FROM 'DE' is TRUE, and NULL IS DISTINCT FROM NULL is FALSE. It's also the right tool for "has this column changed?" checks in triggers and merges.
Never write = NULL. Use IS NULL / IS NOT NULL.
The dangerous one: NOT IN with a NULL
SELECT * FROM customers
WHERE id NOT IN (SELECT customer_id FROM blocked_customers);
If any customer_id in the subquery is NULL, this returns no rows at all. x NOT IN (1, 2, NULL) means x <> 1 AND x <> 2 AND x <> NULL, and the last part is always UNKNOWN.
Use NOT EXISTS, which isn't affected by NULLs and is often faster:
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM blocked_customers b WHERE b.customer_id = c.id);
Aggregates and NULL
COUNT(*)counts rows;COUNT(col)counts non-null values.SUM,AVG,MIN,MAXignoreNULLs.AVG(rating)is the average of rated rows only, which is often what you want, but not always.SUMover zero rows (or onlyNULLs) returnsNULL, not 0. UseCOALESCE(SUM(x), 0).GROUP BYputs allNULLs in one group, even thoughNULL = NULLis unknown.
Arithmetic and strings
Almost any expression with a NULL input is NULL:
price * quantity -- NULL if either is NULL
first_name || ' ' || last_name -- NULL in PostgreSQL and standard SQL if any part is NULL (Oracle treats NULL as '' here)
CONCAT(first_name, ' ', last_name) -- MySQL: NULL if any argument is NULL; PostgreSQL/SQL Server: treats NULL as ''
Use COALESCE(col, default) where a default makes sense. (Oracle also treats the empty string '' as NULL, a famous portability trap.)
Uniqueness and sorting
- A
UNIQUEconstraint allows manyNULLs in most databases, becauseNULLs aren't equal to each other. PostgreSQL 15+ lets you choose withUNIQUE NULLS NOT DISTINCT. SQL Server is the odd one out: a unique index allows only oneNULLunless you use a filtered index. ORDER BYputsNULLs first or last depending on the database. Be explicit where you can (ORDER BY col NULLS LASTin PostgreSQL and Oracle).
Design: fewer NULLs, fewer surprises
- Make columns
NOT NULLunless "unknown" is a real state you need. - Use explicit defaults (
0,'',false) when those mean the same thing as "none" in your domain, andNULLonly when unknown and none must be told apart. - Document what
NULLmeans for each nullable column: "not provided", "not applicable" or "not yet known".
Checklist
- Comparisons with
NULLareUNKNOWN, and filters drop them, so<>silently excludesNULLs. - Use
IS NULL,IS DISTINCT FROMor<=>for null-aware comparisons. - Replace
NOT IN (subquery)withNOT EXISTS. - Remember that aggregates skip
NULLs,SUMof nothing isNULL, andNOT NULLcolumns prevent most of this.
Get the weekly commit
New database deep dives every week.
