In IB Computer Science, students learn that SQL can summarise data using aggregate functions such as COUNT, SUM, and AVG. However, these summaries become far more powerful when combined with GROUP BY. GROUP BY allows databases to organise data into categories and calculate results for each category separately.
IB examiners often test whether students understand what GROUP BY actually does, not just where it appears in a query.
What Is GROUP BY?
The GROUP BY clause is used to:
- Divide records into groups
- Perform calculations on each group
- Return one result per group
Instead of producing a single overall result, GROUP BY produces multiple summary results, one for each group.
In IB terms, GROUP BY supports categorised data analysis.
Why GROUP BY Is Needed
Without GROUP BY:
- Aggregate functions return one value for the entire table
With GROUP BY:
- Aggregate functions return one value per category
For example:
- Total students per year group
- Average score per subject
- Number of orders per customer
GROUP BY allows databases to answer “per category” questions.
How GROUP BY Works Conceptually
Conceptually, GROUP BY works as follows:
- Records are divided into groups based on a field
- Each group is processed separately
- Aggregate functions are applied to each group
