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
order_lines
line_idunit_pricequantitydiscount
12035
2152NULL
34010
412510
Result
line_idunit_pricequantitydiscountline_total
1203555
2152NULLNULL
3401040
41251050

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.

order_lines
line_idunit_pricequantitydiscount
12035
2152NULL
34010
412510

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
Result
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
Result
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
Result
revenue
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.

ExpressionResultWhy
`x + 1`NULLArithmetic needs both values
`x > 0`NULLA comparison with an unknown side is unknown
`ABS(x)`NULLMost functions return NULL for NULL input
`x IS NULL`TRUETests for NULL, so returns TRUE or FALSE
`COALESCE(x, 0)`0Replaces NULL with the first known value
`FALSE AND x > 0`FALSEFALSE settles an AND by itself
`TRUE OR x > 0`TRUETRUE 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 rowsRow skippedAggregates 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

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.