0
0
MySQLquery~10 mins

GROUP BY with multiple columns in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GROUP BY with multiple columns
Start with SELECT query
Identify columns to GROUP BY
Sort rows by first column
Within first column groups, sort by second column
Aggregate rows in each group
Return grouped result set
The query groups rows first by the first column, then by the second column, aggregating data within each group.
Execution Sample
MySQL
SELECT department, role, COUNT(*) AS count
FROM employees
GROUP BY department, role;
This query counts employees grouped by both department and role.
Execution Table
StepRow DataGroup Key (department, role)ActionAggregate Count
1{department: 'Sales', role: 'Manager'}('Sales', 'Manager')Create new group1
2{department: 'Sales', role: 'Salesperson'}('Sales', 'Salesperson')Create new group1
3{department: 'Sales', role: 'Manager'}('Sales', 'Manager')Add to existing group2
4{department: 'HR', role: 'Manager'}('HR', 'Manager')Create new group1
5{department: 'HR', role: 'Recruiter'}('HR', 'Recruiter')Create new group1
6{department: 'HR', role: 'Manager'}('HR', 'Manager')Add to existing group2
7{department: 'Sales', role: 'Salesperson'}('Sales', 'Salesperson')Add to existing group2
8No more rows-Finish grouping-
💡 All rows processed and grouped by department and role.
Variable Tracker
Group KeyStartAfter 1After 2After 3After 4After 5After 6After 7Final
('Sales', 'Manager')011222222
('Sales', 'Salesperson')001111122
('HR', 'Manager')000011222
('HR', 'Recruiter')000001111
Key Moments - 3 Insights
Why does the count for ('Sales', 'Manager') increase only on certain steps?
Because only rows with department 'Sales' and role 'Manager' add to that group, as shown in steps 1 and 3 in the execution_table.
What happens when a new combination of department and role appears?
A new group is created for that combination, as seen in steps 2, 4, and 5 where new groups start.
Why do some groups have the same count after multiple steps?
Because no new rows matching those group keys were processed in those steps, so the count stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the aggregate count for ('HR', 'Manager') after step 6?
A1
B3
C2
D0
💡 Hint
Check the 'Aggregate Count' column for step 6 in the execution_table.
At which step does the group ('Sales', 'Salesperson') first get created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for the first 'Create new group' action with group key ('Sales', 'Salesperson') in the execution_table.
If a new row with {department: 'HR', role: 'Manager'} is added after step 7, what would happen to the count?
ACount for ('Sales', 'Manager') increases by 1
BCount for ('HR', 'Manager') increases by 1
CA new group is created
DNo change in any count
💡 Hint
Refer to variable_tracker for ('HR', 'Manager') and how counts increase when matching rows are added.
Concept Snapshot
GROUP BY with multiple columns syntax:
SELECT columns, aggregate_function()
FROM table
GROUP BY column1, column2;

Groups rows first by column1, then by column2.
Aggregates data within each unique combination of these columns.
Full Transcript
This visual execution shows how a SQL query with GROUP BY on multiple columns works. The query groups rows by department and role. Each row is checked, and if its department and role combination is new, a new group is created. If the combination exists, the row is added to that group, increasing the count. The execution table tracks each step, showing how groups form and counts increase. The variable tracker summarizes counts for each group after each step. Key moments clarify common confusions about grouping and counting. The quiz tests understanding of group creation and count updates. The snapshot summarizes the syntax and behavior of GROUP BY with multiple columns.