01 / 21
? NULL

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.

SQL

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".

Unknown
A student's grade hasn't been entered yet.
Missing
A customer's phone number was never collected.
Not Applicable
A "spouse name" field for an unmarried person.

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.

Two-Valued Logic TRUE FALSE Three-Valued Logic TRUE FALSE UNKNOWN SQL's model

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.

SELECT * FROM students
WHERE grade > 90;

-- If grade is NULL for a student,
-- NULL > 90 = UNKNOWN
-- That row is silently excluded!

The Core Rule of NULL

\( 5 = \text{NULL} \)
UNKNOWN
\( \text{NULL} = \text{NULL} \)
UNKNOWN
\( \text{NULL} > 100 \)
UNKNOWN

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
TRUETRUETRUE
TRUEUNKNOWNUNKNOWN
TRUEFALSEFALSE
UNKNOWNUNKNOWNUNKNOWN
UNKNOWNFALSEFALSE
FALSEFALSEFALSE

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
TRUETRUETRUE
TRUEUNKNOWNTRUE
TRUEFALSETRUE
UNKNOWNUNKNOWNUNKNOWN
UNKNOWNFALSEUNKNOWN
FALSEFALSEFALSE

NOT and UNKNOWN

The NOT operator flips TRUE to FALSE and vice versa. But it cannot resolve uncertainty: NOT UNKNOWN is still UNKNOWN.

NOT TRUE FALSE NOT FALSE TRUE NOT UNKNOWN UNKNOWN

= NULL vs IS NULL

Wrong: = NULL
\( \text{WHERE grade} = \text{NULL} \)
This always evaluates to UNKNOWN. It will never match any row, even rows where grade actually is NULL.
Correct: IS NULL
\( \text{WHERE grade IS NULL} \)
This is the special operator designed to test for NULL. It returns TRUE or FALSE, never UNKNOWN.

How to Handle NULLs

1
Identify nullable columns. Check the schema: which columns allow NULL?
2
Use IS NULL / IS NOT NULL to test for the presence or absence of data.
3
Use COALESCE() to provide a default value: \( \text{COALESCE(grade, 0)} \) returns 0 if grade is NULL.
4
Think in three values. Before writing WHERE conditions, ask: "What happens if this column is NULL?"
5
Test edge cases. Always include NULL rows in your test data to verify queries behave correctly.

Example 1: Simple Filter

Given a table with three students, what does this query return?

-- students table:
-- | 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:

SELECT name FROM students
WHERE grade <> 95;

Example 3: AND / OR with NULL

Evaluate this expression when grade is NULL and attendance is TRUE:

WHERE grade > 80 AND attendance = TRUE
\( \text{NULL} > 80 \)
UNKNOWN AND
\( \text{TRUE} = \text{TRUE} \)
TRUE UNKNOWN AND TRUE = UNKNOWN

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.

COUNT(*)
Counts all rows, including those with NULLs. Result: 3
COUNT(grade)
Counts only non-NULL values. Result: 2
AVG(grade)
Averages only non-NULL values: \( \frac{95 + 80}{2} = 87.5 \), not \( \frac{95 + 0 + 80}{3} \)

Common Mistakes

Using = NULL
Writing \( \text{WHERE x} = \text{NULL} \) instead of \( \text{WHERE x IS NULL} \). The first always returns zero rows.
Assuming NOT removes NULLs
Writing \( \text{WHERE NOT (x} > 5) \) and expecting NULL rows to appear. NOT UNKNOWN is still UNKNOWN.
Forgetting NULLs in NOT IN
If a subquery returns any NULL, \( \text{NOT IN} \) returns UNKNOWN for every row and the query returns nothing.

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} \)?