Aggregates & groups
See the bigger picture
Lesson 7 of 20 · 7 min · Module 3: See patterns in data
Aggregate functions turn many rows into a useful summary. Count records, sum values, or calculate an average.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Group rows sharing a value
- Count records within a group
- Distinguish an aggregate from a raw field
The idea
COUNT(*) counts rows. SUM(xp) adds values. Use GROUP BY to calculate a separate result for each group.
SELECT language, SUM(xp)
FROM learners
GROUP BY language;
01Group by the question’s category
GROUP BY language combines learners with the same profile language. COUNT(*) then produces one count for each group, so the result has one row per language.
02Choose coherent result columns
Select the grouping key and an aggregate value. Selecting another ungrouped, non-aggregated column can give a misleading or database-specific result.
Your turn
language and COUNT(*) for each language in the learners table. Group by language.What the checks look for
- Counts two JavaScript, two Python, and one SQL learner
Take this with you
Grouping changes what one result row represents.