Summarising data: COUNT, SUM, AVG, MIN and MAX
Summarise many rows into one answer with SQL aggregate functions COUNT, SUM, AVG, MIN and MAX, count distinct values, and combine aggregates with WHERE.
🎯 By the end of this lesson you will be able to:
- Use COUNT, SUM, AVG, MIN and MAX to summarise a table
- Explain the difference between COUNT(*) and COUNT(column)
- Combine aggregates with WHERE to summarise part of a table
Outline
What you'll learn and why it matters
Managers rarely want to see every row. They want answers: “How many orders did we get?”, “What is our average price?”, “When was the last order?”. Aggregate functions turn many rows into a single summary value. They are the heart of every report and dashboard.
Counting rows with COUNT
SELECT COUNT(*) AS total_orders
FROM orders;Result: 1 row
| total_orders |
|---|
| 15 |
COUNT(*) counts rows. The result is a single row with a single value, however big the table is.
COUNT(column) counts only the rows where that column is not NULL. Compare:
SELECT COUNT(*) AS customers, COUNT(email) AS with_email
FROM customers;Result: 1 row
| customers | with_email |
|---|---|
| 10 | 8 |
There are 10 customers, but only 8 have an email: the two NULLs from the last lesson are skipped. COUNT(DISTINCT column) counts different values:
SELECT COUNT(DISTINCT customer_id) AS buyers
FROM orders;Result: 1 row
| buyers |
|---|
| 8 |
15 orders were placed by 8 different customers.
SUM, AVG, MIN and MAX
| Function | Gives | Ignores NULL? |
|---|---|---|
SUM(column) | the total | yes |
AVG(column) | the average (mean) | yes |
MIN(column) | the smallest value (or earliest date, or first in A to Z) | yes |
MAX(column) | the largest value (or latest date, or last in A to Z) | yes |
SELECT MIN(price) AS cheapest,
MAX(price) AS dearest,
ROUND(AVG(price), 2) AS average
FROM products;Result: 1 row
| cheapest | dearest | average |
|---|---|---|
| 90 | 1200 | 394.63 |
SELECT SUM(quantity) AS items_ordered
FROM orders;Result: 1 row
| items_ordered |
|---|
| 25 |
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;Result: 1 row
| first_order | last_order |
|---|---|
| 2025-09-01 | 2025-10-15 |
Summarising part of a table
WHERE runs before the aggregate, so the summary only covers the rows that pass the filter:
SELECT COUNT(*) AS delivered
FROM orders
WHERE status = 'delivered';Result: 1 row
| delivered |
|---|
| 10 |
SELECT COUNT(*) AS products, ROUND(AVG(price), 2) AS avg_price
FROM products
WHERE category = 'Electronics';Result: 1 row
| products | avg_price |
|---|---|
| 2 | 749.5 |
Practice
Guided exercise
Stationery summary
How many stationery products are there, and what is their total price if you bought one of each?
- Use
COUNT(*)andSUM(price)with clear aliases. - Filter with
WHERE category = 'Stationery'.
Show the solution
SELECT COUNT(*) AS products, SUM(price) AS one_of_each
FROM products
WHERE category = 'Stationery';Result: 1 row
| products | one_of_each |
|---|---|
| 2 | 210 |
How to get there: The filter keeps the Notebook (120) and the Gel Pens (90); SUM adds their prices.
Your turn
Orders in October
Count the orders placed in October 2025 and the total number of items in them.
Show the solution
SELECT COUNT(*) AS orders, SUM(quantity) AS items
FROM orders
WHERE order_date BETWEEN '2025-10-01' AND '2025-10-31';Result: 1 row
| orders | items |
|---|---|
| 5 | 6 |
How to get there: BETWEEN with two dates includes both ends. You could also write order_date >= '2025-10-01' AND order_date < '2025-11-01', which still works if times are added to the dates later.
Common mistakes
Using COUNT(column) when you mean all rows
You see: A count lower than expected
Fix: Use COUNT(*) to count rows; COUNT(column) skips NULLs.
Putting an aggregate in WHERE: WHERE SUM(quantity) > 5
You see: misuse of aggregate: SUM()
Fix: WHERE runs before aggregating. Use HAVING (next lesson).
Mixing an ordinary column with an aggregate
You see: An error in most databases, or a misleading row in SQLite
Fix: Add GROUP BY (next lesson) or remove the ordinary column.
Averaging whole numbers in other databases
You see: An average that has lost its decimals
Fix: Multiply by 1.0 first if your database does integer division.
Recap
- Aggregates turn many rows into one value: COUNT, SUM, AVG, MIN, MAX.
COUNT(*)counts rows;COUNT(column)skips NULLs;COUNT(DISTINCT column)counts different values.- WHERE filters rows before they are summarised.
- Without GROUP BY, an aggregate query returns exactly one row.
Key terms
- Aggregate function
- A function that summarises many rows into one value.
- Mean
- The usual average: the total divided by the number of values.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. A table has 10 rows and 3 of them have NULL in `phone`. What is `COUNT(phone)`?
2. Which query finds the date of the most recent order?
3. How many rows does `SELECT AVG(price) FROM products WHERE price > 100;` return?
4. When does WHERE run in an aggregate query?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.