COUNT, SUM, AVG, MIN and MAX over a live table, and the one thing they all do with NULL that catches people out.
Aggregate Functions and the NULL Trap
Same table, four questions
query.sqlSQLite
SELECT COUNT(*) AS rows_total,
COUNT(temp_c) AS rows_with_a_reading,
SUM(temp_c) AS total,
AVG(temp_c) AS mean,
MIN(temp_c) AS coldest,
MAX(temp_c) AS warmest
FROM readings;
Result
Worth knowing
An aggregate collapses many rows into one value. Without GROUP BY the whole table is one group.
COUNT(*) counts rows. COUNT(col) counts rows where col is not NULL. The difference is your missing data.
Every other aggregate skips NULL silently. AVG divides by the number of values it actually saw, not by the number of rows.
An aggregate over zero values is NULL, not zero — except COUNT, which is 0.
Overview
An aggregate calculates a value from several rows. COUNT, SUM, AVG, MIN, and MAX are common examples. They can summarize a whole result or one group at a time. The important detail is which rows each function sees, especially when a column contains NULL.
An aggregate calculates a value from several rows. COUNT, SUM, AVG, MIN, and MAX are common examples. They can summarize a whole result or one group at a time. The important detail is which rows each function sees, especially when a column contains NULL.
COUNT(*) counts rows, including rows with missing values. COUNT(amount) counts only rows where amount is not NULL. SUM(amount) and AVG(amount) also skip NULL values. MIN and MAX compare the non-NULL values. These rules mean that a count of rows and a count of known values can differ.
query.sqlSQLite
SELECT COUNT(*) AS rows,
COUNT(amount) AS known_amounts,
AVG(amount) AS average_amount
FROM payments;
Result
If the rows contain amounts 10, NULL, and 20, the result is three rows, two known amounts, and an average of 15.
Empty input and groups
An aggregate query without GROUP BY still returns one result row when its input is empty. COUNT(*) returns 0; most other aggregates return NULL because there is no value to combine. COALESCE(SUM(amount), 0) is appropriate only when the application means “no rows” to count as zero.
With GROUP BY, the database produces one output row per distinct grouping key. A group with no matching rows does not appear at all. For filtering groups by an aggregate, use HAVING; use WHERE to remove input rows before they are grouped. See GROUP BY and HAVING.
Averages need a denominator
AVG(amount) is equivalent to the sum of known amounts divided by their count. It is not the same as treating every missing amount as zero. For 10, NULL, and 20, the average is 15; replacing the missing value with zero changes it to 10. Choose the interpretation based on what NULL means in the data.
Weighted totals and joined rows
Do not average already-calculated averages unless each group has the same number of observations. If one class has 5 students averaging 80 and another has 25 averaging 60, the overall average is weighted by 5 and 25, not the midpoint of 80 and 60. Keep each group’s sum and count if you need to combine summaries. Also check join cardinality before summing: a one-to-many join can repeat a value and inflate the total. Define the row grain first, then aggregate at that grain.
SQLite SELECT explains aggregate queries and grouping.
Check yourself
0 of 3
Answer without scrolling back up.
What is the difference between COUNT(*) and COUNT(temp_c)?
The gap between them is exactly the number of missing values in the column, which makes it a one-line data-quality check.
AVG(temp_c) over nine rows, four of them NULL, divides the sum by what?
AVG skips NULLs entirely - it is SUM(col)/COUNT(col). Whether that is the answer you want depends on what NULL means in your data.
What does SUM return over a group containing no non-NULL values?
Every aggregate except COUNT returns NULL over nothing. That NULL then propagates through any arithmetic it feeds, which is why COALESCE turns up around aggregates so often.
Cheat sheet
Aggregate Functions and the NULL Trap
Run COUNT, SUM, AVG, MIN and MAX on real rows, see how each one handles NULLs, and fix the silent mistake that changes your totals.
Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.