NULL: working with missing values
Understand SQL NULL values, find them with IS NULL and IS NOT NULL instead of =, and replace them in results with COALESCE.
🎯 By the end of this lesson you will be able to:
- Explain what NULL means and why `= NULL` never matches
- Find missing values with IS NULL and IS NOT NULL
- Show a replacement for missing values with COALESCE
Outline
What you'll learn and why it matters
Real data always has gaps: a customer skips the email field, a delivery date is not known yet. Databases mark a missing value as NULL. NULL behaves differently from every other value, and misunderstanding it is one of the most common causes of wrong answers in SQL, so it deserves its own lesson.
What NULL means
NULL means unknown or missing. It is not zero, and it is not empty text (''). In our shop, Meera and Sara have no email, and Kofi's city is unknown.
Finding NULLs with IS NULL
Because NULL is unknown, comparing anything with = to NULL gives “unknown” rather than true, so this finds nothing:
SELECT name, email
FROM customers
WHERE email = NULL;Result: 0 rows
| name | |
|---|---|
| (no rows) | |
Always use IS NULL instead:
SELECT name, email
FROM customers
WHERE email IS NULL;Result: 2 rows
| name | |
|---|---|
| Meera Nair | NULL |
| Sara Lopez | NULL |
And IS NOT NULL for rows that do have a value:
SELECT name, city
FROM customers
WHERE city IS NOT NULL AND country = 'India'
ORDER BY name;Result: 4 rows
| name | city |
|---|---|
| Arjun Singh | Delhi |
| Asha Patel | Mumbai |
| Meera Nair | Kochi |
| Ravi Kumar | Bengaluru |
Replacing NULL in results with COALESCE
COALESCE(a, b) gives a if it is not NULL, otherwise b. Use it to show something friendlier than a blank:
SELECT name, COALESCE(email, 'no email') AS contact
FROM customers
WHERE country = 'India';Result: 4 rows
| name | contact |
|---|---|
| Asha Patel | asha@example.com |
| Ravi Kumar | ravi@example.com |
| Meera Nair | no email |
| Arjun Singh | arjun@example.com |
This only changes the result, not the table. You can give COALESCE more than two values; it returns the first one that is not NULL.
Where NULL surprises people
| Situation | What happens | Why |
|---|---|---|
WHERE email = NULL | No rows | Comparisons with NULL are unknown, never true |
WHERE city <> 'Mumbai' | Kofi (city NULL) is not included | NULL <> 'Mumbai' is unknown, so the row is dropped |
price + NULL | NULL | Any arithmetic with an unknown value is unknown |
COUNT(email) | Counts only non-NULL emails | Covered in the next lesson |
Practice
Guided exercise
Customers without a city
Show the name and country of customers whose city is missing.
- Select
name, countryfromcustomers. - Filter with
city IS NULL, not= NULL.
Show the solution
SELECT name, country
FROM customers
WHERE city IS NULL;Result: 1 row
| name | country |
|---|---|
| Kofi Mensah | Ghana |
How to get there: Only Kofi has no city. city = NULL would have returned no rows at all.
Your turn
Friendly city list
Show the name and city of customers from Ghana and the UK, with Unknown instead of a blank city. Call the column city.
Show the solution
SELECT name, COALESCE(city, 'Unknown') AS city
FROM customers
WHERE country IN ('Ghana', 'UK');Result: 2 rows
| name | city |
|---|---|
| John Smith | London |
| Kofi Mensah | Unknown |
How to get there: COALESCE returns London for John, because it is not NULL, and the fallback text for Kofi.
Common mistakes
Writing = NULL or <> NULL
You see: Empty results, no error
Fix: Use IS NULL and IS NOT NULL.
Treating NULL as zero or empty text
You see: Totals or filters that are slightly wrong
Fix: Remember NULL means unknown. Use COALESCE(column, 0) if you really want zero.
Forgetting NULL rows in a not-equal filter
You see: Rows with missing values silently disappear
Fix: Add OR column IS NULL when those rows should count.
Recap
- NULL means a value is missing or unknown. It is not 0 and not ''.
- Find it with
IS NULL/IS NOT NULL;= NULLnever matches. COALESCE(column, fallback)shows a replacement for NULL in results.- NULL makes comparisons and arithmetic unknown, so check how your filters treat it.
Key terms
- IS NULL
- The test for a missing value.
- COALESCE
- A function that returns the first of its values that is not NULL.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which filter finds customers with no email?
2. What is `COALESCE(NULL, 'Pune', 'Delhi')`?
3. Kofi's city is NULL. Is Kofi returned by `WHERE city <> 'Mumbai'`?
4. What does NULL mean in a price column?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.