Nishaglobal Education logo

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.

Lesson 6 of 1225 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Counting rows with COUNT
  3. SUM, AVG, MIN and MAX
  4. Summarising part of a table
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

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

SQL
SELECT COUNT(*) AS total_orders
FROM orders;

Result: 1 row

Query result
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:

SQL
SELECT COUNT(*) AS customers, COUNT(email) AS with_email
FROM customers;

Result: 1 row

Query result
customerswith_email
108

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:

SQL
SELECT COUNT(DISTINCT customer_id) AS buyers
FROM orders;

Result: 1 row

Query result
buyers
8

15 orders were placed by 8 different customers.

SUM, AVG, MIN and MAX

FunctionGivesIgnores NULL?
SUM(column)the totalyes
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
SQL
SELECT MIN(price) AS cheapest,
       MAX(price) AS dearest,
       ROUND(AVG(price), 2) AS average
FROM products;

Result: 1 row

Query result
cheapestdearestaverage
901200394.63
SQL
SELECT SUM(quantity) AS items_ordered
FROM orders;

Result: 1 row

Query result
items_ordered
25
SQL
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;

Result: 1 row

Query result
first_orderlast_order
2025-09-012025-10-15

Summarising part of a table

WHERE runs before the aggregate, so the summary only covers the rows that pass the filter:

SQL
SELECT COUNT(*) AS delivered
FROM orders
WHERE status = 'delivered';

Result: 1 row

Query result
delivered
10
SQL
SELECT COUNT(*) AS products, ROUND(AVG(price), 2) AS avg_price
FROM products
WHERE category = 'Electronics';

Result: 1 row

Query result
productsavg_price
2749.5

Practice

Guided exercise

Stationery summary

How many stationery products are there, and what is their total price if you bought one of each?

  1. Use COUNT(*) and SUM(price) with clear aliases.
  2. Filter with WHERE category = 'Stationery'.
Show the solution
Solution
SELECT COUNT(*) AS products, SUM(price) AS one_of_each
FROM products
WHERE category = 'Stationery';

Result: 1 row

Query result
productsone_of_each
2210

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
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

Query result
ordersitems
56

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. 1. A table has 10 rows and 3 of them have NULL in `phone`. What is `COUNT(phone)`?

  2. 2. Which query finds the date of the most recent order?

  3. 3. How many rows does `SELECT AVG(price) FROM products WHERE price > 100;` return?

  4. 4. When does WHERE run in an aggregate query?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.