0
0
SQLquery~10 mins

GROUP BY with aggregate functions in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GROUP BY with aggregate functions
Start with table data
Group rows by column(s)
Apply aggregate functions (SUM, COUNT, AVG, etc.) on each group
Return one row per group with aggregated values
End with grouped result set
The query groups rows by specified columns, then calculates aggregate values for each group, returning one summary row per group.
Execution Sample
SQL
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Counts how many employees are in each department by grouping rows by department.
Execution Table
StepActionGroup formedAggregate calculationOutput row
1Read all rows from employees tableNone yetNone yetNone yet
2Group rows by departmentSales: 3 rows, HR: 2 rows, IT: 2 rowsNone yetNone yet
3Calculate COUNT(*) for Sales groupSalesCOUNT = 3('Sales', 3)
4Calculate COUNT(*) for HR groupHRCOUNT = 2('HR', 2)
5Calculate COUNT(*) for IT groupITCOUNT = 2('IT', 2)
6Return all grouped rows with countsAll groupsAll counts calculated[('Sales', 3), ('HR', 2), ('IT', 2)]
7End of query executionAll groups processedAll aggregates doneFinal result set returned
💡 All groups processed and aggregate counts calculated for each group.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
groupsNone{Sales:3 rows, HR:2 rows, IT:2 rows}{Sales:3}{Sales:3, HR:2}{Sales:3, HR:2, IT:2}{Sales:3, HR:2, IT:2}
aggregatesNoneNone{Sales:3}{Sales:3, HR:2}{Sales:3, HR:2, IT:2}{Sales:3, HR:2, IT:2}
output_rowsNoneNone[('Sales', 3)][('Sales', 3), ('HR', 2)][('Sales', 3), ('HR', 2), ('IT', 2)][('Sales', 3), ('HR', 2), ('IT', 2)]
Key Moments - 3 Insights
Why does the query return fewer rows than the original table?
Because the GROUP BY groups multiple rows into one per group, the output has one row per unique group value, as shown in execution_table rows 3-6.
What happens if we use an aggregate function without GROUP BY?
The aggregate function applies to the whole table as one group, returning a single summary row, unlike the multiple groups shown in the execution_table.
Can we select columns not in GROUP BY or aggregate functions?
No, SQL requires all selected columns to be either grouped or aggregated, otherwise it causes an error. This is why only 'department' and COUNT(*) appear in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the COUNT(*) for the HR group at step 4?
A2
B3
C1
D0
💡 Hint
Check the 'Aggregate calculation' column at step 4 in the execution_table.
At which step does the query return the final grouped result set?
AStep 2
BStep 3
CStep 6
DStep 1
💡 Hint
Look for the step where 'Return all grouped rows with counts' happens in the execution_table.
If the employees table had a new department 'Marketing' with 4 employees, how would the output change?
AThe output would remain the same
BA new group 'Marketing' with COUNT 4 would appear
CThe counts for existing groups would increase
DThe query would fail
💡 Hint
Refer to variable_tracker showing how groups and counts form per department.
Concept Snapshot
GROUP BY groups rows by specified columns.
Aggregate functions (COUNT, SUM, AVG, etc.) calculate summary values per group.
Result returns one row per group with aggregated data.
All selected columns must be grouped or aggregated.
Useful for summarizing data by categories.
Full Transcript
This visual execution trace shows how a SQL query with GROUP BY and aggregate functions works. First, the database reads all rows from the table. Then it groups rows by the specified column, here 'department'. Next, it calculates aggregate values like COUNT for each group. Finally, it returns one row per group with the aggregated results. The execution table tracks each step, showing groups formed and counts calculated. The variable tracker shows how groups and aggregates build up over steps. Key moments clarify common confusions like why output rows are fewer than input rows and the need for grouping or aggregation in SELECT. The quiz tests understanding of counts per group, when results are returned, and how adding data affects output. The snapshot summarizes the concept simply for quick review.