Module 3 · Sorting and summarising

Create per-group summaries with GROUP BY

By the end you will be able to obtain a separate summary for each group.

About 14 min

0 of 52 lessons

Before you start, you need

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

Produce one summary row per group

One figure for all orders does not tell you how many are pending or shipped. GROUP BY splits the rows into groups so a summary can be calculated within each one.

GROUP BY status gathers the orders that share the same status. Then COUNT(*) counts how many there are in each group. The result shows one row for pending, one for shipped and one for cancelled.

What a row represents changes: before, it was an order; now it is a status with its count. That is why we select status and the aggregate, rather than the id of an individual order.

The column you write in GROUP BY is the one that separates the groups. You can sort the summaries with ORDER BY, but that does not change which orders belong to each group.

Minimal syntax
SELECT column_name, COUNT(*) AS quantity
FROM table_name
GROUP BY column_name;

See it in action

How many orders are there in each status?

Sample data: 4 rows
idstatus
101shipped
102pending
103cancelled
104shipped

Before running: After grouping by status, will a row represent an order or a status?

SQL
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
ORDER BY status;
Example result: 3 rows
statusorders
cancelled3
pending6
shipped9
  1. Each input row represents an order. GROUP BY status gathers the orders that share the same status.
  2. COUNT(*) is calculated within each group. Each result row now represents a status.
  3. ORDER BY status sorts those summaries. It does not change which orders took part in each group.
  4. Select the columns that define the group and their aggregates. A single date does not represent every order in a status.

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

Count the orders in each status

Not started

Count how many orders there are in each status. Return status and the count aliased as orders; sort the rows by status.

Expected result: 3 rows with the columns status, orders, in that order. Keep the row order requested.

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

Summarise another property

Not started

Show category and COUNT(*) aliased as products, from products. Sort by category.

Expected result: 5 rows with the columns category, products, in that order. Keep the row order requested.

View tables and data

Open this panel to see the full data.

Key takeaways

  • GROUP BY gathers rows that share values.
  • Aggregates are calculated within each group.
  • Each final row represents a group.

Next: Filter groups with HAVING