Learn SQL Lesson

Summarizing with GROUP BY

`GROUP BY` collects rows into groups before applying aggregate functions.

For example, to total sales by region:

SELECT region, SUM(amount) AS total_amount
FROM sales
GROUP BY region

Now `SUM(amount)` runs once per region instead of once for the whole table.

Remember this rule: when you use `GROUP BY`, every selected column must either be grouped or aggregated.

Practice challenge

Show total sales by region. Return region and total_amount, ordered by total_amount descending.

Open interactive editor