Before you start, you need
Filter rows with WHERE · Use aliases and create calculated columns · Work correctly with NULL values
Summarise a set in one row
So far the result has shown individual items. To find out how many orders there are or what they add up to, you need to summarise several rows in a single figure. The functions that produce that summary are called aggregate functions.
COUNT(*) counts rows. SUM(total) adds up their amounts. AVG(total) calculates the average: it adds up the values and divides by how many there are. MIN and MAX return the smallest and the largest. With 10, 20 and 30, the sum is 60 and the average is 20.
Without GROUP BY, these functions produce a summary row. You can use WHERE beforehand to choose which orders take part. Give each figure an alias that explains what it measures. ROUND(figure, 2) rounds the result to two decimal places.
COUNT(email) counts only the emails that are present; COUNT(*) counts every customer. SUM, AVG, MIN and MAX also skip NULL. If there are no values present, they return NULL; counts return 0.
Do not confuse rows with quantities: a product with 30 units is one row, even though its stock is 30. Choose the function according to what you want to measure.
SELECT COUNT(*) AS quantity,
SUM(total) AS amount
FROM orders;See it in action
How many orders are there, and what do they add up to?
SELECT COUNT(*) AS orders,
ROUND(SUM(total), 2) AS amount
FROM orders;| orders | amount |
|---|---|
| 18 | 3931.9 |
- COUNT(*) counts every row in orders. SUM(total) adds up their amounts. The result has a single row.
- ROUND(..., 2) shows the amount with two decimal places. AS tells the count apart from the amount.
- Do not select the name of an individual order next to this summary: an overall figure has no particular row that represents it.
- For other questions, AVG calculates the average, and MIN and MAX return the extremes. Choose the measure by what it means.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Add up and average orders
In a single row, calculate the sum and the average of total for orders that are not cancelled. Round both to two decimal places and use the aliases total and average.
Expected result: 1 row with the columns total, average, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
You need a total amount and an average for the orders that are not cancelled.
Exclude status = 'cancelled' with WHERE before applying SUM and AVG.
Round each aggregate with ROUND(..., 2) and use total and average as aliases.
Solution explained
SELECT ROUND(SUM(total), 2) AS total, ROUND(AVG(total), 2) AS average FROM orders WHERE status <> 'cancelled';WHERE filters the rows first, and the aggregates summarise only the remaining orders.
Count presence and rows
Guided practice
Count presence and rows
Return one row with COUNT(*) as customers and COUNT(email) as with_email from customers.
Expected result: 1 row with the columns customers, with_email, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
If there are 12 customers and 3 missing emails, what do COUNT(*) and COUNT(email) return?
Ask for both figures in the same query: COUNT(*) counts rows and COUNT(email) counts the emails that are present.
The aliases are customers and with_email: COUNT(*) AS customers, COUNT(email) AS with_email.
Solution explained
SELECT COUNT(*) AS customers, COUNT(email) AS with_email FROM customers;There are 12 rows and 9 addresses present. COUNT(email) ignores the three NULLs.
Check an empty input
Guided practice
Check an empty input
Filter products with stock below 0 and return COUNT(*) as rows and SUM(stock) as units.
Expected result: 1 row with the columns rows, units, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
If no row meets stock < 0, what do COUNT(*) and SUM(stock) return?
Filter with WHERE before calculating COUNT(*) and SUM(stock).
The aliases are rows and units. Notice that SUM returns NULL, not 0, when there are no rows.
Solution explained
SELECT COUNT(*) AS rows, SUM(stock) AS units FROM products WHERE stock < 0;There is no negative stock: the output is one row with 0 and NULL. The sum of an empty input is not a recorded stock of 0.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Find the extremes
Return one row from products with the lowest price as minimum and the highest as maximum.
Expected result: 1 row with the columns minimum, maximum, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
You do not need one row per product: you are looking for two values from the set.
Use the functions that find extremes.
The required aliases are minimum and maximum.
Solution explained
SELECT MIN(price) AS minimum, MAX(price) AS maximum FROM products;The two functions summarise the whole catalogue in one row.
Key takeaways
- COUNT counts; SUM adds up; AVG calculates an average.
- MIN and MAX return the extremes.
- COUNT(column) skips NULL; COUNT(*) counts rows.
- Without GROUP BY, the whole set is summarised in one row.