Sub topic 9
How NULL Affects Filters
This chapter assumes you know basic SELECT statements, tables, columns, comparison operators such as =, <, and >, and AND and OR in a WHERE clause.
NULL Means “Unknown”
NULL is a special SQL marker for a missing or unknown value. It is not the number zero, an empty string, or the text "NULL".
A comparison involving NULL usually produces UNKNOWN:
SELECT
NULL = NULL AS equal_result,
NULL <> 10 AS not_equal_result,
NULL > 10 AS greater_result;SQL uses three-valued logic:
TRUEmeans the condition matches.FALSEmeans the condition does not match.UNKNOWNmeans SQL cannot determine the result.
A WHERE clause keeps only rows for which its condition is TRUE. Both FALSE and UNKNOWN are filtered out.
Testing for NULL
Use IS NULL to find missing values and IS NOT NULL to find known values:
SELECT id, shipped_at
FROM orders
WHERE shipped_at IS NULL;
SELECT id, shipped_at
FROM orders
WHERE shipped_at IS NOT NULL;The first query finds orders that have not recorded a shipping timestamp. It does not test whether shipped_at equals a particular value.
The following condition never finds a null value:
WHERE shipped_at = NULLThe correct test is:
WHERE shipped_at IS NULLNULL with AND and OR
UNKNOWN also affects combined conditions.
| Condition | Result |
|---|---|
| `TRUE AND UNKNOWN` | `UNKNOWN` |
| `FALSE AND UNKNOWN` | `FALSE` |
| `TRUE OR UNKNOWN` | `TRUE` |
| `FALSE OR UNKNOWN` | `UNKNOWN` |
For example, a row with a null salary does not satisfy this condition:
WHERE salary > 50000
AND department = 'Sales'The salary comparison is UNKNOWN, so the whole AND expression cannot be TRUE.
An OR condition can still match when one side is true:
WHERE department = 'Sales'
OR salary > 50000A sales employee with a null salary matches because the first condition is TRUE.
The `NOT IN` NULL Trap
NOT IN can produce surprising results when its list contains NULL. SQL must check that a value is different from every item. A comparison with the null item becomes UNKNOWN, so the complete condition is not TRUE.
SELECT id
FROM employees
WHERE department_id NOT IN (10, 20, NULL);This can return no rows, even when department_id is neither 10 nor 20.
The same problem can occur with a subquery if the subquery returns a null value:
SELECT c.id, c.name
FROM customers AS c
WHERE c.id NOT IN (
SELECT o.customer_id
FROM orders AS o
);If any orders.customer_id is NULL, the result may contain no customers. Prefer NOT EXISTS when checking for the absence of related rows:
SELECT c.id, c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);NOT LIKE and NOT BETWEEN have the same basic behavior. If the tested column is NULL, the positive comparison is UNKNOWN, and negating it remains UNKNOWN. The row is therefore excluded.
Practical Rules
- Use
IS NULLandIS NOT NULLfor null tests. - Expect ordinary comparisons with
NULLto becomeUNKNOWN. - Check subquery results before using
NOT IN. - Use
NOT EXISTSwhen related rows may contain null foreign keys. - Add an explicit null condition when your business rule requires it, such as
salary IS NULL OR salary < 50000.
Exercise
Why does WHERE discount <> 0 exclude products whose discount is NULL, and how can you include those products?
Show answer
NULL <> 0 is UNKNOWN, and WHERE keeps only TRUE. Use WHERE discount <> 0 OR discount IS NULL.