Module 3 · Sorting and summarising

Summarise data with COUNT, SUM, AVG, MIN and MAX

By the end you will be able to count rows and calculate sums, means and extremes.

About 16 min

0 of 52 lessons

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.

Minimal syntax
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?

SQL
SELECT COUNT(*) AS orders,
       ROUND(SUM(total), 2) AS amount
FROM orders;
Example result: 1 rows
ordersamount
183931.9
  1. COUNT(*) counts every row in orders. SUM(total) adds up their amounts. The result has a single row.
  2. ROUND(..., 2) shows the amount with two decimal places. AS tells the count apart from the amount.
  3. Do not select the name of an individual order next to this summary: an overall figure has no particular row that represents it.
  4. 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

Not started

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.

Count presence and rows

Guided practice

Count presence and rows

Not started

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.

Check an empty input

Guided practice

Check an empty input

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Find the extremes

Not started

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.

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.

Next: Create per-group summaries with GROUP BY