01 / 21
?

NULL &
Missingness

Learn why missing data behaves differently from every other value in SQL, and how to handle it with confidence.

SQL Database Programming

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?

Incorrect Counts
Aggregate functions like COUNT(column) silently skip NULLs, giving you smaller totals than expected.
Broken Filters
A WHERE clause comparing to NULL never returns true, so matching rows seem to vanish.
Silent Errors
Math with NULL produces NULL, so calculations can propagate missing data without warning.

Visualizing NULL in a Table

idnameemailscore
1Alicealice@mail.com92
2BobNULL85
3Carolcarol@mail.comNULL
4DaveNULLNULL

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.

TRUE FALSE UNKNOWN
SQL's Three Truth Values
\(\texttt{5 = 5}\)
\(\texttt{5 = 3}\)
\(\texttt{5 = NULL}\)

\(\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.

-- This returns ZERO rows, even if email IS NULL!
SELECT * FROM students
WHERE email = NULL; -- Always UNKNOWN
-- Correct way to find NULLs:
SELECT * FROM students
WHERE email IS NULL; -- ✓ Returns matching rows

IS NULL vs IS NOT NULL

IS NULL
Returns TRUE when the value is missing. This is the only correct way to check for NULLs.
IS NOT NULL
Returns TRUE when the value exists. Use it to filter for rows that have actual data.

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 rows4
COUNT(score)Skips NULLs2
SUM(score)Skips NULLs177
AVG(score)Skips NULLs88.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.

SELECT name,
    COALESCE(email, 'no-email@unknown.com') AS email
FROM students;
nameemail
Alicealice@mail.com
Bobno-email@unknown.com
Carolcarol@mail.com
Daveno-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 NULLUNKNOWNFALSEUNKNOWN
X OR NULLTRUEUNKNOWNUNKNOWN
NOT NULLUNKNOWN

How to Handle NULLs

1
Identify nullable columns. Check which columns in your table allow NULLs by reading the schema or using DESCRIBE.
2
Test for NULLs explicitly. Use IS NULL or IS NOT NULL in your WHERE clause, never = or !=.
3
Choose COUNT carefully. Use COUNT(*) for total rows, COUNT(column) for non-NULL values only.
4
Provide defaults. Wrap nullable expressions in COALESCE(value, default) to prevent NULL propagation.
5
Verify aggregates. After computing AVG or SUM, check if NULLs changed the denominator or total unexpectedly.

Example 1: Finding Missing Data

Task: Find all students who have not provided an email address.

SELECT id, name
FROM students
WHERE email IS NULL;
idname
2Bob
4Dave

Example 2: Safe Calculations

Task: Calculate a bonus of 10% of each student's score. Treat missing scores as 0.

SELECT name,
    COALESCE(score, 0) * 0.10 AS bonus
FROM students;
namebonus
Alice9.2
Bob8.5
Carol0.0
Dave0.0

Example 3: The COUNT Trap

Task: A teacher wants to know how many students submitted scores. Watch the difference:

-- Counts ALL rows (4)
SELECT COUNT(*) FROM students; -- Result: 4

-- Counts only non-NULL scores (2)
SELECT COUNT(score) FROM students; -- Result: 2

Common Mistakes

✕ Using \(\texttt{= NULL}\)
Writing \(\texttt{WHERE x = NULL}\) always yields UNKNOWN, returning zero rows. Use IS NULL instead.
✕ Confusing NULL with ''
An empty string '' is a real value. NULL means no value at all. They behave differently in filters and aggregates.
✕ Forgetting NULL Propagation
Any arithmetic with NULL gives NULL. \(5 + \texttt{NULL} = \texttt{NULL}\). Always wrap with COALESCE if you need a numeric result.

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 rowsCOUNT(column)COUNT(*)
Default for NULLUse raw columnCOALESCE(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?