NULL propagation
Concepts covered: Null Propagation
Almost every operator and function returns NULL when any of its inputs is NULL. NULL + 5, NULL * 0, NULL > 3 and ABS(NULL) all come out NULL, because the engine cannot compute an answer from a value it does not have. Each operator follows that rule on its own, and the damage comes from chaining them. A derived column built from 4 inputs is NULL whenever any one of them is, and every column derived from it inherits the NULL, step after step down a pipeline.
order_lines holds the 4 lines of an order. Line 2 came through a checkout version that writes NULL instead of 0 when no discount applies. Trace line 2 through the expression before running the query.
SELECT
line_id,
unit_price,
quantity,
discount,
unit_price * quantity - discount AS line_total
FROM order_lines
ORDER BY line_id| line_id | unit_price | quantity | discount |
|---|---|---|---|
| 1 | 20 | 3 | 5 |
| 2 | 15 | 2 | NULL |
| 3 | 40 | 1 | 0 |
| 4 | 12 | 5 | 10 |
| line_id | unit_price | quantity | discount | line_total |
|---|---|---|---|---|
| 1 | 20 | 3 | 5 | 55 |
| 2 | 15 | 2 | NULL | NULL |
| 3 | 40 | 1 | 0 | 40 |
| 4 | 12 | 5 | 10 | 50 |
unit_price * quantity for line 2 is a known 30. Subtracting NULL turns it into NULL, and the 30 is gone from the row. One stage later, SUM skips that NULL like any other, so the order's revenue loses line 2 completely and the query raises no error. Each tab below sums the 4 lines a different way.
| line_id | unit_price | quantity | discount |
|---|---|---|---|
| 1 | 20 | 3 | 5 |
| 2 | 15 | 2 | NULL |
| 3 | 40 | 1 | 0 |
| 4 | 12 | 5 | 10 |
No fix
Sums the raw expression, and SUM skips line 2's NULL total.
SELECT
SUM(unit_price * quantity - discount) AS revenue
FROM order_lines| revenue |
|---|
| 145 |
Fix output
Wraps the computed total in COALESCE, so line 2 contributes 0.
SELECT
SUM(COALESCE(unit_price * quantity - discount, 0)) AS revenue
FROM order_lines| revenue |
|---|
| 145 |
Fix input
Wraps the discount in COALESCE, so line 2's total is computed from known parts.
SELECT
SUM(unit_price * quantity - COALESCE(discount, 0)) AS revenue
FROM order_lines| revenue |
|---|
| 175 |
- 145
- 175
Wrapping the total repairs nothing. By the time COALESCE sees line 2, the subtraction has already thrown away the price and quantity, so COALESCE swaps NULL for 0, and 0 adds to the sum exactly what a skipped row adds. A repair has to sit where the NULL enters, on the input whose NULL has a known meaning. Here a missing discount means no discount, so 0 is correct. A NULL quantity would be different: nobody knows how many items shipped, and defaulting it to 0 would write a guess into the revenue.
Build the revenue query for order_lines, where line 2's discount is NULL because no discount applied. Put COALESCE where the NULL enters, so the 4 lines sum to 175.
Where the spread stops
Some expressions absorb a NULL instead of passing it on. Knowing which ones lets you trace a NULL forward and name every output column it reaches. In the table, x is NULL.
| Expression | Result | Why |
|---|---|---|
| `x + 1` | NULL | Arithmetic needs both values |
| `x > 0` | NULL | A comparison with an unknown side is unknown |
| `ABS(x)` | NULL | Most functions return NULL for NULL input |
| `x IS NULL` | TRUE | Tests for NULL, so returns TRUE or FALSE |
| `COALESCE(x, 0)` | 0 | Replaces NULL with the first known value |
| `FALSE AND x > 0` | FALSE | FALSE settles an AND by itself |
| `TRUE OR x > 0` | TRUE | TRUE settles an OR by itself |
| `CASE WHEN x > 0 THEN 'pos' ELSE 'other' END` | 'other' | An unknown condition takes the ELSE branch |
| `SUM(x)` across rows | Row skipped | Aggregates ignore NULL inputs |
The CASE row hides the NULL completely. A label such as CASE WHEN discount > 0 THEN 'discounted' ELSE 'full price' END files line 2 under full price. The NULL never appears in the output, so nothing downstream can tell that the label came from a guess. When a NULL input should stay visible, give it a branch of its own, WHEN discount IS NULL THEN 'unknown', placed first.
readings has a row where temp_c is NULL and alert_on is TRUE. What does status hold for that row?
SELECT
CASE
WHEN alert_on AND temp_c > 40 THEN 'alarm'
WHEN NOT (temp_c > 40) THEN 'normal'
ELSE 'check sensor'
END AS status
FROM readings- 'alarm', because alert_on is TRUE and that settles the first condition
- 'normal', because temp_c > 40 is not TRUE, so its NOT must be TRUE
- 'check sensor', because both conditions are NULL and ELSE runs
- NULL, because the unknown temperature makes the whole CASE NULL
About this lesson
This section is part of the NULL Values: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.