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| order_id | amount | discount |
|---|---|---|
| ORD-001 | 100 | 10 |
| ORD-002 | 200 | NULL |
| ORD-003 | 150 | 15 |
| ORD-004 | 300 | NULL |
| ORD-005 | 250 | 25 |
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| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| avg_when_recorded | avg_per_order |
|---|---|
| 16.666666666666668 | 10 |
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| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| smallest_discount | largest_discount |
|---|---|
| 10 | 25 |
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')| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| rows_kept | discounts_found | discount_total |
|---|---|---|
| 2 | 0 | NULL |
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.
| Aggregate | On 10, NULL, 15, NULL, 25 | On NULL, NULL |
|---|---|---|
| COUNT(*) | 5 | 2 |
| COUNT(discount) | 3 | 0 |
| SUM(discount) | 50 | NULL |
| AVG(discount) | 16.67 | NULL |
| MIN(discount) | 10 | NULL |
| MAX(discount) | 25 | NULL |
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| order_id | channel | amount | discount |
|---|---|---|---|
| ORD-001 | web | 100 | 10 |
| ORD-002 | web | 200 | NULL |
| ORD-003 | web | 150 | 15 |
| ORD-004 | app | 300 | NULL |
| ORD-005 | app | 250 | 25 |
| channel | all_orders | discounted_orders | total_discount | avg_discount |
|---|---|---|---|---|
| app | 2 | 1 | 25 | 25 |
| web | 3 | 2 | 25 | 12.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- 2, because the total of 8 is divided by all 4 rows
- NULL, because a single NULL in the column makes the average unknown
- 4, because the total of 8 is divided by the 2 recorded ratings
- 8, because the NULL rows are skipped and the ratings are added
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.