Part 03 / Topic 3 of 9
NULL and three-valued logic
NULL means "unknown / not recorded". It is not zero, not empty text, not false. Sara's city is NULL: we don't know where she lives — that is different from her living in a city called nothing.
Because NULL is unknown, anything combined with it is also unknown:
SELECT NULL = NULL AS equals_null,
NULL + 1 AS null_plus_one,
NULL IS NULL AS is_null,
NULL IS DISTINCT FROM 1 AS distinct_from_1;
equals_null | null_plus_one | is_null | distinct_from_1
-------------+---------------+---------+-----------------
| | t | t
(1 row)
Even NULL = NULL is NULL — "is one unknown equal to another unknown? Unknown." SQL logic has three values: true, false, unknown — and WHERE keeps only true rows. That is why Part 2's Pune filter skipped Sara in both directions.
The operator that exists precisely for this is IS NULL (and IS NOT NULL):
SELECT first_name, city FROM customers WHERE city IS NULL;
first_name | city
------------+------
Sara |
(1 row)
SELECT first_name, city FROM customers WHERE city IS NOT NULL ORDER BY first_name;
first_name | city
------------+---------
Asha | Pune
John | London
Meera | Chennai
Ravi | Delhi
Vikram | Pune
(5 rows)
An empty cell in psql output is NULL. (Tip for your own sessions: \pset null '∅' makes psql print NULLs visibly.)
🧪 Quick quiz — answer from memory, then check.
- What does
SELECT NULL = NULL;return, and why? - How do you find rows where
cityhas no value?
Show the answers
- NULL — comparing two unknowns yields unknown, not true.
WHERE city IS NULL— never= NULL, which is always unknown.
Done here? The practice set for Part 3 — 7 exercises with solutions — is on the part page.