Sub topic 4
Reading NULLs in Outer Join Results
Assume you know SELECT, WHERE, NULL, primary and foreign keys, and INNER JOIN. This chapter focuses on what NULL means after an outer join and how to use it safely.
An outer join preserves rows that an inner join would discard. When a preserved row has no matching row on the other table, SQL fills the missing table's columns with NULL. This is called NULL padding.
Suppose these tables contain the following data:
| Customer | Order ID | Total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
Cleo appears because the query uses LEFT JOIN. The NULL values do not mean that Cleo has an order whose ID is unknown. They mean that no order row matched Cleo.
NULL means “unknown or missing”
SQL uses NULL to represent an absent or unknown value. It is not the same as zero, an empty string, or the text "NULL".
You cannot test for NULL with = or <>. SQL uses IS NULL and IS NOT NULL instead.
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
-- CleoThis query finds customers with no matching order. o.order_id is a NOT NULL primary-key column whenever an order row exists, so a NULL value here identifies the padded row.
The reverse test finds customers with at least one matching order:
SELECT DISTINCT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL;
-- Ada
-- BenNULL also affects ordinary comparisons. A condition such as o.total > 30 is neither true nor false when o.total is NULL. It is unknown. The WHERE clause keeps only rows where the condition is true, so the row for Cleo is removed.
Replacing NULL for display
You can replace a NULL value with a fallback using COALESCE. COALESCE returns its first non-NULL argument.
SELECT c.name,
COALESCE(o.order_id, 0) AS displayed_order_id,
COALESCE(o.total, 0.00) AS displayed_total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id;This can make a report easier to read, but use it carefully. Replacing a missing order ID with 0 changes presentation, not the underlying meaning. There is still no order with ID zero.
For calculations, COALESCE is especially useful. SUM(o.total) returns NULL for a customer with no matching order, so use COALESCE(SUM(o.total), 0) when the report should show zero spending.
Follow the NULL through the query
A column can also be NULL in a real row. For example, orders.customer_id may be NULL because an order was created without an assigned customer. Therefore, choose a right-side column that cannot be NULL when identifying a missing row, usually its primary key.
A common mistake
This query looks like a left join, but its WHERE clause removes padded rows:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.total > 30;Because o.total > 30 is unknown for Cleo, Cleo disappears. If the goal is to keep every customer while showing only orders above 30, put the filter in ON:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
AND o.total > 30;Now Cleo remains with NULL order columns.
Exercise
Which condition finds customers without orders: o.order_id = NULL or o.order_id IS NULL? Why?
Show answer
o.order_id IS NULL. SQL requires IS NULL because comparisons with NULL produce unknown rather than true.