Find a course

Module 3: Summarizing DataLesson 5 of 1112 min

Aggregate Functions: COUNT, SUM, AVG, MIN and MAX

Summarize many rows into one answer with COUNT, SUM, AVG, MIN and MAX, and learn how COUNT(*) and COUNT(column) treat NULL differently.

Step 1 of 14Learn

From rows to answers

Most business questions aren't about individual rows. They're about totals and averages: How many books do we carry? What's our total inventory? What's the average price? Aggregate functions collapse many rows into a single value.

FunctionReturns
COUNT(*)the number of rows
COUNT(column)the number of non-NULL values in the column
SUM(column)the total
AVG(column)the average (mean)
MIN(column)the smallest value
MAX(column)the largest value

Using the books table:

SELECT COUNT(*) AS num_books,
       MIN(price) AS cheapest,
       MAX(price) AS priciest,
       AVG(price) AS avg_price
FROM books;

Result: one row with num_books = 8, cheapest = 12.99, priciest = 42.00, and avg_price = 182.49 ÷ 8 ≈ 22.81.

MIN and MAX also work on text (alphabetical order) and dates (earliest and latest): MIN(order_date) gives the first order date.

Aggregates combine naturally with WHERE, which filters rows before they are aggregated:

SELECT COUNT(*) AS scifi_books, AVG(price) AS avg_scifi_price
FROM books
WHERE genre = 'Sci-Fi';

Two Sci-Fi books, averaging (18.00 + 21.00) ÷ 2 = 19.50.

You can also aggregate expressions: SUM(price * stock) gives the total retail value of the inventory. Many databases offer rounding with ROUND(AVG(price), 2) to keep results tidy.

Step 2 of 14Diagram

Five aggregate functions on the books table

Each one collapses many rows into a single value. Tap a function to see its result on the sample data.

Five aggregate functions on the books tableParts diagram: Aggregate functions, COUNT, SUM, AVG, MIN, MAX.AggregatefunctionsCOUNTSUMAVGMINMAX

Tap a shape with to see more.

Aggregate functions
COUNT

COUNT(*) = 8 rows. COUNT(stock) = 7, because one stock is NULL.

SUM

SUM(price) = 182.49, the total of all eight prices.

AVG

AVG(price) ≈ 22.81, the sum divided by the number of non-NULL values.

MIN

MIN(price) = 12.99 (Night Ferry).

MAX

MAX(price) = 42.00 (Data in Practice).

Step 3 of 14Note

Step 4 of 14Practice · Work it out

Using the books table, what does this query return? SELECT SUM(stock) FROM books;

Step 5 of 14Note

Step 6 of 14Practice · Chart

Ten orders, one outlier

Nine orders sit near $30 and one is $3,000. AVG(total) for these ten is about $327, far above every typical order.

Which order pulls the average far away from what a typical order looks like?

Tap the bar that answers the question, then press Check.

Ten orders, one outlierBar chart with 10 categories: O1: $28; O2: $31; O3: $29; O4: $30; O5: $32; O6: $27; O7: $33; O8: $30; O9: $29; O10: $3,000.$0$1,000$2,000$3,000Order totalO1O2O3O4O5O6O7O8O9O10$28$31$29$30$32$27$33$30$29$3,000
Illustrative numbers for teaching. Mean ≈ $327; median = $30.
Ten orders, one outlier
ItemOrder total
O1$28
O2$31
O3$29
O4$30
O5$32
O6$27
O7$33
O8$30
O9$29
O10$3,000

Step 7 of 14Learn

COUNT(*) versus COUNT(column), and how NULLs are handled

**All aggregate functions except COUNT() ignore NULL values.* This single fact explains many puzzling results.

SELECT COUNT(*) AS all_rows,
       COUNT(stock) AS known_stock,
       COUNT(DISTINCT author) AS authors
FROM books;

Returns all_rows = 8, known_stock = 7 and authors = 5.

  • COUNT(*) counts rows, whatever they contain.
  • COUNT(stock) counts only rows where stock is not NULL, so Quiet Numbers is left out.
  • COUNT(DISTINCT author) counts unique non-NULL values.

The same rule affects averages. AVG(stock) is 74 ÷ 7 ≈ 10.57, because it divides by the 7 known values, not by 8. If you believe missing stock should count as zero, you'd write AVG(COALESCE(stock, 0)), giving 74 ÷ 8 = 9.25. Which is right depends on what NULL means in your data; the important thing is to make the choice deliberately.

The customers table shows the same effect: COUNT(*) is 5, but COUNT(email) is 4, because Tom's email is NULL. That difference is itself useful: "how many customers are missing an email?" is COUNT(*) - COUNT(email), which equals 1.

A clever pattern combines aggregates with CASE to count several things at once:

SELECT
  SUM(CASE WHEN stock = 0 THEN 1 ELSE 0 END) AS sold_out,
  SUM(CASE WHEN price > 20 THEN 1 ELSE 0 END) AS over_20
FROM books;

Result: sold_out = 1, over_20 = 4. Each CASE turns a condition into 1 or 0, and SUM counts the 1s.

Step 8 of 14Compare

Three ways to count the books table

AspectCOUNT(*)COUNT(stock)COUNT(DISTINCT author)
CountsEvery rowRows where stock is not NULLDifferent non-NULL authors
NULLsIncludedSkippedSkipped
DuplicatesIncludedIncludedCounted once
Result875

Step 9 of 14Practice · Match the pairs

Match each query on the books table to its result.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. SELECT COUNT(*) FROM books;
  2. SELECT COUNT(stock) FROM books;
  3. SELECT COUNT(DISTINCT genre) FROM books;
  4. SELECT MAX(year) FROM books;

Step 10 of 14Practice · Work it out

Using the books table, what does this query return? Round to two decimal places. SELECT AVG(price) FROM books WHERE genre = 'Tech';

Step 11 of 14Learn

What you can and cannot select alongside aggregates

Here's a query beginners often try:

SELECT title, MAX(price)
FROM books;

The intent is clear: show the most expensive book's title and price. But it's not valid in standard SQL, and most databases reject it with an error. (A few, such as SQLite and some MySQL configurations, accept it and return a title, which can mislead you.)

The problem: MAX(price) collapses 8 rows into one value, but title still has 8 different values. Which title should go in the single result row? SQL refuses to guess.

The rule: in a query with aggregates, every selected column must either be inside an aggregate function or listed in GROUP BY (next lesson).

To get the title of the most expensive book, sort and limit:

SELECT title, price
FROM books
ORDER BY price DESC
LIMIT 1;

or use a subquery, which you'll meet in the joins module:

SELECT title, price
FROM books
WHERE price = (SELECT MAX(price) FROM books);

The subquery version has an advantage: if two books tie for the highest price, it returns both, while LIMIT 1 arbitrarily picks one.

Also note that aggregates cannot be used in WHERE: WHERE price > AVG(price) is an error, because WHERE runs row by row before any aggregation happens. The subquery pattern solves this too: WHERE price > (SELECT AVG(price) FROM books) returns the books priced above average.

Step 12 of 14Practice · Select all that apply

Which of these queries are valid in standard SQL? Select all that apply.

Select all that apply.

Step 13 of 14Practice · True or false

AVG(stock) on the books table divides by 8, because the table has 8 rows.

Step 14 of 14Practice · Choose one

Using the customers table, which query counts how many customers have no email on file?