NULLs and CASE
Handle missing values correctly and compute labels with CASE and COALESCE.
17 min
NULL means unknown or missing. It is not zero and not an empty string. Our customers table has NULL in email for Chen, Elif and Hugo: we do not have it.
Any comparison with NULL is neither true nor false: it is unknown, and WHERE keeps only true rows. So:
WHERE email = NULL -- returns nothing, always
WHERE email <> 'x' -- also drops the NULL rows
WHERE email IS NULL -- the right way
WHERE email IS NOT NULL5 + NULL is NULL. COUNT(email), SUM, AVG, MIN, MAX skip NULLs, while COUNT(*) counts rows. With 8 customers and 3 missing emails: COUNT(*) is 8, COUNT(email) is 5.
COALESCE(a, b, c) returns the first value that is not NULL:
SELECT name, COALESCE(email, 'no email') AS contact FROM customers;SELECT title,
CASE
WHEN price < 12 THEN 'budget'
WHEN price < 20 THEN 'standard'
ELSE 'premium'
END AS band
FROM books;CASE checks the WHENs top to bottom and uses the first that is true, like if / elif / else. Without ELSE, no match gives NULL.
Comparisons with NULL
NULL = NULL
- Result
- NULL (unknown), not true
NULL IS NULL
- Result
- true
5 + NULL
- Result
- NULL
COUNT(col) where col is NULL
- Result
- not counted
COALESCE(NULL, 0)
- Result
- 0
NULL AND false
- Result
- false
NULL OR true
- Result
- true
A WHERE clause keeps a row only when the condition is true. Unknown is dropped.
Show the name of every customer who has no email address on file.
Tables you can query
- customers (id, name, city, joined, email)
SELECT name FROM customers
WHEREShow each customer's name and a column contact holding their email, or the text no email when it is missing.
Tables you can query
- customers (id, name, city, joined, email)
SELECT name,
FROM customersShow each book's title and a band: 'budget' if the price is under 12, 'standard' if under 20, otherwise 'premium'.
Tables you can query
- books (id, title, author, genre, price, year, stock)
SELECT title,
FROM books“`WHERE email <> '[email protected]'` returns every customer except Ben.”
In everyday logic, anyone who is not Ben is "not equal to Ben".
What is actually true
A row with a NULL email gives "unknown", not true, so Chen, Elif and Hugo are dropped too. To include them: WHERE email <> '[email protected]' OR email IS NULL.
How to catch yourself
Whenever a column may hold NULL, ask yourself what should happen to those rows.
- NULL means unknown: use
IS NULLandIS NOT NULL, never= NULL. - A WHERE keeps only true rows, so unknown comparisons drop the row.
- Aggregates skip NULLs;
COUNT(*)counts rows andCOUNT(col)counts non-NULL values. COALESCEsupplies a default, andCASEcomputes a value by rules.
NULL checks.
Which finds rows where email is missing?
- A`WHERE email = NULL`
- B`WHERE email IS NULL`
- C`WHERE email = ''`
- D`WHERE NOT email`
With 8 customers and 3 NULL emails, what does COUNT(email) return?
What does COUNT(*) return for those same 8 customers?
What is 5 + NULL?
- A5
- B0
- CNULL
- DAn error
What does COALESCE(NULL, NULL, 7, 9) return?
- ANULL
- B7
- C9
- D16
Think of a form you have filled in with optional fields. What would "missing" mean for each field, and what would you want a report to do with those rows?
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.