NULL Explained: The Value That Means Unknown
Summary: NULL is SQL's way of saying "no value" or "unknown". It is not zero, not an empty string and not false. Because it means unknown, comparisons with it behave in surprising ways until you learn three simple rules.
In short: test with IS NULL, not = NULL; remember that most calculations with NULL give NULL; and use NOT NULL on columns that must always have a value.
What NULL means
Picture a form with a field for "middle name". Some people leave it blank because they have none; some because they did not want to say. A database stores that missing value as NULL. It is a marker for "nothing recorded here", not a value like 0 or an empty piece of text.
CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL, phone TEXT);
INSERT INTO people (name) VALUES ('Lena'); -- phone is NULL
Rule 1: use IS NULL, never = NULL
SELECT name FROM people WHERE phone = NULL; -- returns nothing, always
SELECT name FROM people WHERE phone IS NULL; -- returns Lena
Why? Asking "is this unknown value equal to unknown?" has no clear yes. SQL answers unknown, and WHERE only keeps rows where the answer is true. So = NULL never matches. IS NULL and IS NOT NULL are special tests made for exactly this.
Rule 2: NULL spreads through calculations
Almost any arithmetic or text joining with NULL gives NULL:
SELECT 5 + NULL; -- NULL
SELECT 'Hi ' || NULL; -- NULL in most databases
If one price in a calculation is missing, the result is missing too. That is usually correct, but it surprises beginners.
Rule 3: functions treat NULL in their own way
| Expression | Result | Why |
|---|---|---|
COUNT(*) | Counts every row | Counts rows, not values |
COUNT(phone) | Counts rows where phone is not NULL | Skips unknowns |
SUM(amount) | Adds the known amounts | NULLs are skipped |
AVG(score) | Average of known scores only | NULLs are not treated as zero |
Replacing NULL for display
COALESCE returns the first value that is not NULL:
SELECT name, COALESCE(phone, 'no phone given') AS phone FROM people;
Handy for reports and screens.
NULL in joins
A LEFT JOIN fills in NULL where nothing matched on the right side. That is useful: WHERE right_table.id IS NULL finds rows with no match, such as customers with no orders.
When to forbid NULL
Add NOT NULL to columns that must always have a value: names, prices, the date an order was placed. The database will then refuse rows that leave them out. Allow NULL only where "unknown" or "not applicable" is a real possibility, such as an optional phone number or a "date returned" for a book not yet returned.
Common slips
- Writing
= NULLor<> NULLin WHERE. - Storing the text
'NULL'or an empty string instead of a real NULL, so IS NULL does not find it. - Forgetting that
NOT INwith a list containing NULL can return no rows at all. PreferNOT EXISTSfor that kind of check. - Assuming averages include missing values as zero.
Next steps
Practise IS NULL in the training notes on WHERE and ORDER BY.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.