Sub topic 8
Why Rows with NULL Keys Disappear
Prerequisites and the matching rule
This chapter assumes that you know basic SELECT, FROM, WHERE, and ORDER BY syntax, and that you understand primary keys and foreign keys.
An inner join returns only pairs of rows for which the ON condition is true. Consider the sample tables:
customers.ididentifies a customer.orders.customer_ididentifies the customer who placed an order.orders.customer_idcan beNULL, meaning that the order has no recorded customer.
The value NULL does not mean zero, an empty string, or a customer with ID zero. It means that the value is missing or unknown.
Equality with NULL
Suppose the data includes this order:
| id | customer_id | total |
|---|---|---|
| 104 | NULL | 99.00 |
This query tries to match every order to a customer:
SELECT c.name, o.id AS order_id, o.customer_id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;For order 104, SQL evaluates a condition like this for every customer:
NULL = 1
NULL = 2
NULL = 3None of these comparisons is TRUE. In SQL, a comparison involving NULL produces UNKNOWN. SQL uses three logical results:
TRUEFALSEUNKNOWN
An inner join keeps a pair only when the ON condition is TRUE. Therefore, order 104 has no matching pair and disappears from the result.
The same rule applies when the NULL value is on the primary-key side, although a normal primary key cannot contain NULL. A nullable join column on either side prevents an equality match.
`NULL` is different from a missing row
These two situations have different meanings:
- An order row exists, but its
customer_idisNULL. - A customer row exists, but that customer has no orders.
An inner join excludes both situations because each requires a missing match. The first order has no customer row to match. The second customer has no order row to match.
If you need to keep the order or customer anyway, use an outer join. For example, a left join preserves every customer and places NULL in the order columns when no order matches:
SELECT c.name, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
ORDER BY c.id, o.id;A left join does not make NULL = NULL true. It preserves the unmatched left-side row after the join finds no true match.
Testing for missing keys
To find orders with no recorded customer, test explicitly with IS NULL:
SELECT id, total
FROM orders
WHERE customer_id IS NULL;Do not write customer_id = NULL. That condition evaluates to UNKNOWN, so a WHERE clause rejects the row.
To find orders whose customer key is present, use:
SELECT id, customer_id
FROM orders
WHERE customer_id IS NOT NULL;Some database systems support a null-safe comparison such as IS NOT DISTINCT FROM. It treats two NULL values as equal:
SELECT *
FROM left_table AS l
INNER JOIN right_table AS r
ON l.key IS NOT DISTINCT FROM r.key;This is useful when two missing values should represent a match, but it is not the usual meaning of a nullable foreign key. Check your database system before using this syntax.
Exercise
Exercise
An orders table contains order 7 with customerid NULL. Will this row appear in an INNER JOIN to customers ON orders.customerid = customers.id? How would you find it?
Show answer
No. The equality condition is UNKNOWN, so the inner join excludes the row. Find it with WHERE customer_id IS NULL.