Three-Valued Logic
& NULL in SQL
Discover why SQL doesn't just deal in TRUE and FALSE: there's a mysterious third value called NULL that changes every rule you know about logic.
What Is NULL?
In SQL, NULL is not a value: it represents the absence of a value. It means the data is unknown, missing, or not applicable. It is not the same as zero, an empty string, or the word "null".
From Two Values to Three
In everyday programming, Boolean logic uses two values: TRUE and FALSE. But SQL introduces a third possibility: UNKNOWN. Any comparison involving NULL produces UNKNOWN instead of TRUE or FALSE.
Why Does This Matter?
The WHERE clause in SQL only returns rows where the condition evaluates to TRUE. Rows that evaluate to FALSE or UNKNOWN are both excluded. This means NULLs can silently cause rows to disappear from your results.
WHERE grade > 90;
-- If grade is NULL for a student,
-- NULL > 90 = UNKNOWN
-- That row is silently excluded!
The Core Rule of NULL
AND with Three Values
When NULL enters an AND expression, think of it as "maybe." If one side is definitely FALSE, the whole thing is FALSE regardless. Otherwise, UNKNOWN propagates.
| A | B | A AND B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | UNKNOWN | UNKNOWN |
| TRUE | FALSE | FALSE |
| UNKNOWN | UNKNOWN | UNKNOWN |
| UNKNOWN | FALSE | FALSE |
| FALSE | FALSE | FALSE |
OR with Three Values
With OR, the opposite rule applies: TRUE dominates. If one side is definitely TRUE, the result is TRUE regardless of the other side.
| A | B | A OR B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | UNKNOWN | TRUE |
| TRUE | FALSE | TRUE |
| UNKNOWN | UNKNOWN | UNKNOWN |
| UNKNOWN | FALSE | UNKNOWN |
| FALSE | FALSE | FALSE |
NOT and UNKNOWN
The NOT operator flips TRUE to FALSE and vice versa. But it cannot resolve uncertainty: NOT UNKNOWN is still UNKNOWN.
= NULL vs IS NULL
This always evaluates to UNKNOWN. It will never match any row, even rows where grade actually is NULL.
This is the special operator designed to test for NULL. It returns TRUE or FALSE, never UNKNOWN.
How to Handle NULLs
Example 1: Simple Filter
Given a table with three students, what does this query return?
-- | name | grade |
-- | Alice | 95 |
-- | Bob | NULL |
-- | Carol | 80 |
SELECT name FROM students
WHERE grade > 85;
Example 2: NOT Equal Trap
You want all students except those with grade 95. Watch what happens:
WHERE grade <> 95;
Example 3: AND / OR with NULL
Evaluate this expression when grade is NULL and attendance is TRUE:
NULL in Aggregates
Aggregate functions like SUM, AVG, MIN, and MAX simply skip NULL values. But COUNT(*) counts all rows, while COUNT(column) skips NULLs.
Common Mistakes
Quick Reference Table
| Expression | Result | Explanation |
|---|---|---|
| \( \text{NULL} = \text{NULL} \) | UNKNOWN | Can't compare unknowns |
| \( \text{NULL} \neq 5 \) | UNKNOWN | Any comparison with NULL |
| \( \text{NULL IS NULL} \) | TRUE | Special null-testing operator |
| \( \text{TRUE AND UNKNOWN} \) | UNKNOWN | Can't confirm both sides |
| \( \text{FALSE AND UNKNOWN} \) | FALSE | FALSE dominates AND |
| \( \text{TRUE OR UNKNOWN} \) | TRUE | TRUE dominates OR |
| \( \text{NOT UNKNOWN} \) | UNKNOWN | Can't negate uncertainty |
| \( \text{COALESCE(NULL, 0)} \) | 0 | Returns first non-NULL |
Question 1 of 5
What does the expression \( \text{NULL} = \text{NULL} \) evaluate to in SQL?
Question 2 of 5
What is the result of \( \text{TRUE AND UNKNOWN} \)?
Question 3 of 5
Given students with grades: 90, NULL, 75. What does \( \text{SELECT COUNT(grade)} \) return?
Question 4 of 5
Which SQL clause correctly finds all rows where the \( \text{email} \) column has no value?
Question 5 of 5
What is the result of \( \text{FALSE OR UNKNOWN} \)?