Lesson 20 / 37 · Grouping

GROUP BY collects rows, then each group becomes one row

Step GROUP BY shows the rows sorted into coloured bands, one per country. Step AGGREGATE collapses each band into a single row. Columns that are not keys and not aggregated are gone after that.

The query

SELECT country, count(*) AS customers
FROM customers
GROUP BY country

Try grouping by region_id, in SELECT and GROUP BY: 4 groups, because GROUP BY puts NULLs together in one group, here Farid's.

Next: Grouping by two keys · All lessons