Sub topic 9
How NULL Affects Filters
This chapter assumes you can write a basic SELECT ... WHERE query, use comparison operators such as =, < and >, and combine conditions with AND and OR. It explains how NULL changes the result of the predicates covered in this tutorial: LIKE, IN and BETWEEN.
What NULL means
NULL marks a missing or unknown value. It is not zero, not an empty string, and not the text 'NULL'. A salary of NULL means "we do not know the salary", not "the salary is 0".
That meaning drives everything else in this chapter. If you ask whether an unknown value equals 50000, the honest answer is "unknown". SQL gives exactly that answer.
Three-valued logic
Most programming languages have two truth values. SQL has three:
TRUEFALSEUNKNOWN
A comparison that involves NULL produces UNKNOWN, not FALSE. This holds for every comparison operator, including =, <>, < and >.
SELECT 1 AS one
WHERE NULL = NULL;This query returns no rows. NULL = NULL is UNKNOWN, because one unknown value cannot be said to equal another. Some databases require a FROM clause. If yours does, use a one-row table, for example FROM employees with a WHERE id = 1 condition added.
What WHERE does with UNKNOWN
WHERE keeps a row only when its condition is TRUE. Rows where the condition is FALSE or UNKNOWN are both discarded. That is why NULL values vanish from filters without any error or warning.
Consider this table:
| id | name | salary |
|---|---|---|
| 1 | Alice | 45000 |
| 2 | Bob | 60000 |
| 3 | Carol | NULL |
SELECT id, name
FROM employees
WHERE salary > 50000;The result is Bob only. For Carol, NULL > 50000 is UNKNOWN, so her row is dropped. Now run the opposite filter:
SELECT id, name
FROM employees
WHERE salary <= 50000;The result is Alice only. Carol is missing again. The two queries together do not cover the whole table. A row with NULL is in neither group.
Testing for NULL
Use IS NULL and IS NOT NULL. These are the only predicates that always return TRUE or FALSE, never UNKNOWN.
SELECT id, name
FROM employees
WHERE salary IS NULL;This returns Carol. Writing salary = NULL returns nothing, because that comparison is always UNKNOWN.
To include missing values in a filter, test for them explicitly:
SELECT id, name
FROM employees
WHERE salary <= 50000
OR salary IS NULL;This returns Alice and Carol.
The truth tables
AND, OR and NOT follow fixed rules when UNKNOWN is involved. Read UNKNOWN as "could be either".
| A | B | A AND B | A OR B |
|---|---|---|---|
| TRUE | UNKNOWN | UNKNOWN | TRUE |
| FALSE | UNKNOWN | FALSE | UNKNOWN |
| UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
| A | NOT A |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
| UNKNOWN | UNKNOWN |
Two rules are worth memorizing:
NOT UNKNOWNis stillUNKNOWN. Negating a filter does not bring back the rows that were dropped for being unknown.FALSE AND UNKNOWNisFALSE, andTRUE OR UNKNOWNisTRUE. When one side already decides the answer, the unknown side does not matter.
NULL inside LIKE, IN and BETWEEN
Each predicate from this tutorial returns UNKNOWN when the value being tested is NULL.
LIKE
SELECT id, name
FROM employees
WHERE name LIKE 'A%';If name is NULL, the pattern test is UNKNOWN and the row is not returned. A NULL pattern also gives UNKNOWN for every row, so name LIKE NULL returns nothing.
IN
IN is shorthand for several equality tests joined by OR. So department IN ('Sales', 'Support') means department = 'Sales' OR department = 'Support'.
- If
departmentisNULL, both comparisons areUNKNOWN, and the whole test isUNKNOWN. The row is dropped. - If the list itself contains a
NULL, a match on another item still givesTRUE. A non-match givesUNKNOWNinstead ofFALSE.
The second point looks harmless in a plain IN. It matters a great deal under NOT, as the next chapter shows.
BETWEEN
x BETWEEN a AND b means x >= a AND x <= b. If x, a or b is NULL, then at least one of the two comparisons is UNKNOWN.
- If the other comparison is
FALSE, the result isFALSE. - If the other comparison is
TRUE, the result isUNKNOWN.
For example, salary BETWEEN 50000 AND NULL with a salary of 40000 gives FALSE, because 40000 >= 50000 is FALSE. With a salary of 60000 the result is UNKNOWN. Either way, no row passes.
Negation does not rescue NULL rows
Because NOT UNKNOWN is UNKNOWN, the negated forms drop NULL rows too:
SELECT id, name
FROM employees
WHERE name NOT LIKE 'A%';A row with a NULL name is not returned. Beginners often expect it to appear, since "it does not start with A". SQL cannot say that about an unknown value. The same applies to NOT IN and NOT BETWEEN.
To include NULL rows in a negated filter, say so:
SELECT id, name
FROM employees
WHERE name NOT LIKE 'A%'
OR name IS NULL;Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `WHERE col = NULL` | Always `UNKNOWN`, no rows returned | `WHERE col IS NULL` |
| `WHERE col <> 'x'` expecting `NULL` rows | Rows with `NULL` are dropped | Add `OR col IS NULL` |
| Counting rows with a filter and its opposite and expecting the total | `NULL` rows appear in neither count | Count `NULL` rows separately |
| `NOT IN` with a list or subquery that may contain `NULL` | No rows returned | Remove `NULL` values, or use `NOT EXISTS` |
Null-safe comparison
Sometimes you want NULL to compare as equal to NULL. The SQL standard provides IS NOT DISTINCT FROM, which returns TRUE when both sides are NULL or both sides are equal.
SELECT id, name
FROM employees
WHERE salary IS NOT DISTINCT FROM 50000;Support varies. PostgreSQL, SQL Server (2022 and later) and Db2 support it. MySQL uses its own operator, <=>. For portable code, spell out the test: (a = b OR (a IS NULL AND b IS NULL)).
COALESCE as a substitute
COALESCE(x, default) returns the first non-NULL argument. It lets you treat missing values as a chosen default:
SELECT id, name
FROM employees
WHERE COALESCE(salary, 0) <= 50000;This returns Alice and Carol, because Carol's missing salary is treated as 0. Use this only when the default is a sensible reading of "unknown". It also usually prevents the database from using an ordinary index on salary, because the column is wrapped in a function.
Checking your data first
Before you write a filter on a column, find out whether it can hold NULL:
SELECT COUNT(*) AS total_rows,
COUNT(salary) AS rows_with_salary,
COUNT(*) - COUNT(salary) AS rows_with_null
FROM employees;COUNT(*) counts every row. COUNT(column) counts only rows where the column is not NULL. The difference is the number of NULL values. If it is greater than zero, decide for each filter whether those rows should be included.
Summary
NULLmeans unknown. Any comparison with it returnsUNKNOWN.WHEREkeeps onlyTRUErows, soFALSEandUNKNOWNrows are both dropped.- Use
IS NULLandIS NOT NULLto test for missing values. LIKE,INandBETWEENreturnUNKNOWNfor aNULLinput, and theirNOTforms do too.- Add explicit
OR ... IS NULLconditions when missing values belong in the result.
Exercise
The employees table has these rows: (1, 'Alice', 45000), (2, 'Bob', 60000), (3, 'Carol', NULL). What does SELECT name FROM employees WHERE NOT (salary > 50000) return, and how would you change it so Carol is also returned?
Show answer
It returns only Alice. For Bob, salary > 50000 is TRUE, so NOT gives FALSE. For Carol, salary > 50000 is UNKNOWN, and NOT UNKNOWN is still UNKNOWN, so her row is dropped. To include Carol, write WHERE NOT (salary > 50000) OR salary IS NULL.
Exercise
Predict the result of WHERE name LIKE 'A%' OR name IS NULL for a row whose name is NULL, and for a row whose name is 'Bob'. Give the truth value of each side and of the whole condition.
Show answer
For the NULL name, name LIKE 'A%' is UNKNOWN and name IS NULL is TRUE, so UNKNOWN OR TRUE is TRUE and the row is kept. For 'Bob', name LIKE 'A%' is FALSE and name IS NULL is FALSE, so the whole condition is FALSE and the row is dropped.