Aggregates with NULLs

Concepts covered: Sums & Averages

SUM and AVG treat a NULL the way COUNT(column) does, and so do MIN and MAX: they read the cells that hold a value and pass over the rest. Nothing warns you and no 0 goes in its place, so the aggregate runs as if the rows with NULL were absent from the table.

Discounts on the 5 orders run 10, NULL, 15, NULL, 25. Before playing the query, work out what you expect the total and the average to be.

SELECT
  SUM(discount) AS total_discount,
  AVG(discount) AS avg_discount
FROM orders
orders
order_idamountdiscount
ORD-00110010
ORD-002200NULL
ORD-00315015
ORD-004300NULL
ORD-00525025

The total is 50: 10 plus 15 plus 25. That number is the same whether you picture the NULLs as skipped or as zeros. The average is where the 2 pictures split. AVG divides 50 by 3, the number of recorded discounts, and returns around 16.67. Dividing by all 5 orders would give 10.

2 averages, 2 different questions

AVG(discount) returns 16.67
  • Divides by 3, the orders with a discount recorded
  • Answers: when a discount is recorded, how big is it?
  • Says nothing about the 2 orders with NULL
SUM(discount) / COUNT(*) returns 10
  • Divides by 5, every order in the table
  • Answers: how much discount does the shop give per order?
  • Treats each NULL as a discount of 0
SELECT
  AVG(discount) AS avg_when_recorded,
  SUM(discount) / COUNT(*) AS avg_per_order
FROM orders
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
avg_when_recordedavg_per_order
16.66666666666666810

Both numbers are correct arithmetic. Which one belongs in a report depends on what a NULL means in this column, the question from the first section. If NULL means the customer used no coupon, the discount was 0 in fact, and 10 is the honest per-order figure. If NULL means the coupon data failed to load for those orders, nobody knows their discounts, and 16.67 is the best estimate from the rows that survived.

MIN and MAX

MIN and MAX compare values to find an extreme, and a NULL can't be compared with anything. So they leave NULLs out of the contest. NULL is never the smallest value in a column and never the largest.

SELECT
  MIN(discount) AS smallest_discount,
  MAX(discount) AS largest_discount
FROM orders
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
smallest_discountlargest_discount
1025

The smallest discount is 10 and the largest is 25. Read the 10 with care: it's the smallest discount that was recorded. If the 2 NULL orders had no coupon, the true minimum across all orders is 0, and MIN has no way to know that.

When every value is missing

Skipping NULLs raises a question: what does an aggregate return when it skips everything? This query keeps only ORD-002 and ORD-004, the 2 orders with no discount.

SELECT
  COUNT(*) AS rows_kept,
  COUNT(discount) AS discounts_found,
  SUM(discount) AS discount_total
FROM orders
WHERE order_id IN ('ORD-002', 'ORD-004')
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
rows_keptdiscounts_founddiscount_total
20NULL

rows_kept is 2 and discounts_found is 0, but discount_total is NULL. SUM had no values to add, so it reports that it has no total, which is different from a total of 0. AVG returns NULL here for the same reason, as do MIN and MAX. COUNT is the one aggregate that answers with a number when nothing is there.

This shows up whenever a filter or a group contains no recorded values. A report cell for a channel with no discounts will show NULL where you may have expected 0.

AggregateOn 10, NULL, 15, NULL, 25On NULL, NULL
COUNT(*)52
COUNT(discount)30
SUM(discount)50NULL
AVG(discount)16.67NULL
MIN(discount)10NULL
MAX(discount)25NULL

Shipping a number people can trust

An aggregate over a column with gaps is a statement about part of the table, and whoever reads your number needs to know how big that part is. Put COUNT(*) and COUNT(column) next to every SUM or AVG you report.

SELECT
  channel,
  COUNT(*) AS all_orders,
  COUNT(discount) AS discounted_orders,
  SUM(discount) AS total_discount,
  AVG(discount) AS avg_discount
FROM orders
GROUP BY channel
orders
order_idchannelamountdiscount
ORD-001web10010
ORD-002web200NULL
ORD-003web15015
ORD-004app300NULL
ORD-005app25025
Result
channelall_ordersdiscounted_orderstotal_discountavg_discount
app212525
web322512.5

Both channels gave away 25 in discounts. The web average is 12.5 across 2 discounted orders out of 3, and the app average is 25 from a single discounted order out of 2. With the counts beside it, nobody will mistake that 25 for a pattern.

Do
  • Report COUNT(*) and COUNT(column) beside every SUM or AVG.
  • Find out what NULL means in a column before averaging it.
  • Use SUM(column) * 1.0 / COUNT(*) when NULL should count as 0; the 1.0 keeps the fraction.
Avoid
  • Assume AVG divides by the number of rows in the table.
  • Expect SUM to return 0 when every value is NULL. It returns NULL.
  • Treat MIN as the smallest value across all rows when some are NULL.

A reviews table has 4 rows. Read from top to bottom, its rating column holds 5, NULL, 3, NULL. What does this query return?

SELECT
  AVG(rating) AS avg_rating
FROM reviews

Every result in this lesson traces back to one decision SQL made: a missing value is never guessed. Comparisons with it are UNKNOWN, filters drop what they cannot confirm, and aggregates work with the values they have. Your job is to know how many rows that left out.

About this lesson

This section is part of the NULL Values: Beginner 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.