0
0
SQLquery~10 mins

How GROUP BY changes query execution in SQL - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How GROUP BY changes query execution
Start with full table data
Scan all rows
Group rows by specified column(s)
Aggregate each group (e.g., COUNT, SUM)
Return one row per group with aggregated values
End
The query first reads all rows, groups them by the chosen column(s), then calculates aggregates for each group, and finally returns one result row per group.
Execution Sample
SQL
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts how many employees are in each department by grouping rows by department.
Execution Table
StepActionInput RowsGroups FormedAggregated ResultOutput Rows
1Scan all rows from employees table10 rowsNone yetNone yetNone yet
2Group rows by department10 rows3 groups: Sales, HR, ITNone yetNone yet
3Count employees in each groupGrouped rows3 groupsSales: 4, HR: 3, IT: 3None yet
4Return one row per group with countsAggregated data3 groupsSame as previous3 rows
5Query endsN/AN/AN/A3 rows
💡 All rows processed and grouped; output contains one row per group with aggregated counts.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Input Rows10 rows from employees10 rowsGrouped into 3 groupsGrouped into 3 groups
GroupsNone3 groups formed3 groups with counts3 groups with counts
Aggregated ResultNoneNoneCounts calculatedCounts calculated
Output RowsNoneNoneNone3 rows returned
Key Moments - 3 Insights
Why does the number of output rows decrease after GROUP BY?
Because rows are grouped by the specified column(s), multiple input rows combine into one group, so the output has one row per group instead of one per input row (see execution_table step 4).
Does GROUP BY change the original data in the table?
No, GROUP BY only organizes rows during query execution; it does not modify the original table data (see execution_table step 1 vs step 2).
What happens if you use GROUP BY without an aggregate function?
The query will return one row per group but without meaningful aggregation, often causing errors or unexpected results depending on SQL rules (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are the rows grouped by department?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Groups Formed' column in execution_table rows.
According to variable_tracker, how many groups exist after Step 3?
ANone
B1 group
C3 groups
D10 groups
💡 Hint
Look at the 'Groups' row after Step 3 in variable_tracker.
If the employees table had 5 departments instead of 3, how would the output rows change?
AOutput rows would be 5
BOutput rows would stay 3
COutput rows would be 10
DOutput rows would be 1
💡 Hint
Refer to execution_table step 4 where output rows equal number of groups.
Concept Snapshot
GROUP BY groups rows by one or more columns.
Aggregates like COUNT or SUM calculate values per group.
Output returns one row per group.
Original table data is not changed.
Without aggregates, GROUP BY may cause errors or unexpected results.
Full Transcript
This visual execution trace shows how the SQL GROUP BY clause changes query execution. First, the query scans all rows from the employees table. Then, it groups these rows by the department column, forming three groups. Next, it calculates the count of employees in each group. Finally, the query returns one row per group with the aggregated counts. The number of output rows is less than the input rows because multiple rows combine into groups. GROUP BY organizes data during query execution but does not modify the original table. This trace helps beginners see step-by-step how grouping and aggregation work together in SQL.