Aggregates and grouping
Summarise many rows into totals with COUNT/SUM/AVG, GROUP BY, and HAVING.
- Compute totals with COUNT, SUM, AVG, MIN, MAX
- Summarise per group with GROUP BY
- Filter groups with HAVING
Aggregate functions collapse many rows into a single value. The common ones:
COUNT, SUM, AVG, MIN, MAX.
You can mix several aggregate functions in one SELECT:
GROUP BY: a total per group
GROUP BY splits rows into groups and computes the aggregate for each. "How many
customers per country?":
The rule: every column in SELECT must either be in the GROUP BY or wrapped
in an aggregate. country is grouped; COUNT(*) is aggregated — valid.
HAVING: filter the groups
WHERE filters rows before grouping; HAVING filters groups after. "Only
countries with more than one customer":
Order of clauses: WHERE (rows) → GROUP BY → HAVING (groups) → ORDER BY.
Use WHERE to cut rows early, HAVING only for conditions on the aggregates.
Where to go next
You've covered reading data. The module's lab puts it all together with graded exercises. (Changing data — INSERT/UPDATE/DELETE — is next, then the lab.)