NULL &
Missingness
Learn why missing data behaves differently from every other value in SQL, and how to handle it with confidence.
What is NULL?
In SQL, NULL is a special marker that represents the absence of a value. It does not mean zero, it does not mean an empty string, and it does not mean false. It means: "we don't know."
Why Does Missingness Matter?
Visualizing NULL in a Table
| id | name | score | |
|---|---|---|---|
| 1 | Alice | alice@mail.com | 92 |
| 2 | Bob | NULL | 85 |
| 3 | Carol | carol@mail.com | NULL |
| 4 | Dave | NULL | NULL |
The Three-Valued Logic Rule
Most programming languages use two-valued logic: TRUE or FALSE. SQL adds a third value: UNKNOWN. Any comparison involving NULL evaluates to UNKNOWN, not TRUE or FALSE.
\(\texttt{NULL = NULL}\) is Not True
This is the most surprising rule for newcomers. Because NULL means "unknown," comparing two unknowns cannot be determined as equal. The database has no idea if the two missing values would match.
SELECT * FROM students
WHERE email = NULL; -- Always UNKNOWN
SELECT * FROM students
WHERE email IS NULL; -- ✓ Returns matching rows
IS NULL vs IS NOT NULL
NULL in Aggregate Functions
SQL aggregate functions handle NULLs in specific, sometimes surprising ways. Understanding this is critical for writing correct queries.
| Function | NULL Behavior | Example Result |
|---|---|---|
| COUNT(*) | Counts all rows | 4 |
| COUNT(score) | Skips NULLs | 2 |
| SUM(score) | Skips NULLs | 177 |
| AVG(score) | Skips NULLs | 88.5 |
Handling NULLs with COALESCE
COALESCE returns the first non-NULL value from its arguments. It is the standard tool for providing default values when data is missing.
COALESCE(email, 'no-email@unknown.com') AS email
FROM students;
| name | |
|---|---|
| Alice | alice@mail.com |
| Bob | no-email@unknown.com |
| Carol | carol@mail.com |
| Dave | no-email@unknown.com |
NULL in Boolean Expressions
When NULL meets AND, OR, and NOT, the three-valued logic applies. Here is the complete truth table:
| Expression | TRUE | FALSE | UNKNOWN |
|---|---|---|---|
| X AND NULL | UNKNOWN | FALSE | UNKNOWN |
| X OR NULL | TRUE | UNKNOWN | UNKNOWN |
| NOT NULL | UNKNOWN | ||
How to Handle NULLs
Example 1: Finding Missing Data
Task: Find all students who have not provided an email address.
FROM students
WHERE email IS NULL;
| id | name |
|---|---|
| 2 | Bob |
| 4 | Dave |
Example 2: Safe Calculations
Task: Calculate a bonus of 10% of each student's score. Treat missing scores as 0.
COALESCE(score, 0) * 0.10 AS bonus
FROM students;
| name | bonus |
|---|---|
| Alice | 9.2 |
| Bob | 8.5 |
| Carol | 0.0 |
| Dave | 0.0 |
Example 3: The COUNT Trap
Task: A teacher wants to know how many students submitted scores. Watch the difference:
SELECT COUNT(*) FROM students; -- Result: 4
-- Counts only non-NULL scores (2)
SELECT COUNT(score) FROM students; -- Result: 2
Common Mistakes
Summary Cheat Sheet
| Situation | Wrong Approach | Correct Approach |
|---|---|---|
| Check for NULL | \(\texttt{x = NULL}\) | x IS NULL |
| Check for not NULL | \(\texttt{x != NULL}\) | x IS NOT NULL |
| Count all rows | COUNT(column) | COUNT(*) |
| Default for NULL | Use raw column | COALESCE(col, default) |
| Math with NULLs | \(\texttt{col} + 10\) | \(\texttt{COALESCE(col, 0)} + 10\) |
Question 1 of 5
What does NULL represent in SQL?
Question 2 of 5
What does the expression \(\texttt{NULL = NULL}\) evaluate to in SQL?
Question 3 of 5
Given a table with 10 rows where 3 rows have NULL in the age column, what does COUNT(age) return?
Question 4 of 5
What does COALESCE(NULL, NULL, 42, 99) return?
Question 5 of 5
Which SQL clause correctly filters for rows where the email column has no value?