0
0
SQLquery~10 mins

GROUP BY single column in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GROUP BY single column
Start with table data
Select column to group by
Scan all rows
Group rows by unique values in that column
Apply aggregate functions (if any) per group
Return grouped result set
The query groups rows by unique values in one column, then aggregates data per group.
Execution Sample
SQL
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query counts employees in each department by grouping rows by department.
Execution Table
StepActionCurrent RowGroup KeyGroups FormedAggregate Count per Group
1Read row('Sales', 'Alice')Sales{Sales}{Sales:1}
2Read row('HR', 'Bob')HR{Sales, HR}{Sales:1, HR:1}
3Read row('Sales', 'Charlie')Sales{Sales, HR}{Sales:2, HR:1}
4Read row('IT', 'David')IT{Sales, HR, IT}{Sales:2, HR:1, IT:1}
5Read row('HR', 'Eve')HR{Sales, HR, IT}{Sales:2, HR:2, IT:1}
6End of rowsN/AN/A{Sales, HR, IT}{Sales:2, HR:2, IT:1}
💡 All rows processed; groups formed by unique department values.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Groups{}{Sales}{Sales, HR}{Sales, HR}{Sales, HR, IT}{Sales, HR, IT}{Sales, HR, IT}
Aggregate Count{}{Sales:1}{Sales:1, HR:1}{Sales:2, HR:1}{Sales:2, HR:1, IT:1}{Sales:2, HR:2, IT:1}{Sales:2, HR:2, IT:1}
Key Moments - 2 Insights
Why does the group 'Sales' count increase on step 3 instead of creating a new group?
Because 'Sales' was already seen in step 1, the row is added to the existing 'Sales' group, increasing its count (see execution_table row 3).
What happens if a row has a department not seen before?
A new group is created for that department, as shown in step 4 with 'IT' (execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the aggregate count for 'HR' after step 5?
A1
B3
C2
D0
💡 Hint
Check the 'Aggregate Count per Group' column at step 5 in the execution_table.
At which step is the 'IT' group first created?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Groups Formed' column to see when 'IT' appears.
If the query did not have GROUP BY, what would happen to the counts?
ACounts would be total for all rows combined
BCounts would be per department as usual
CCounts would be zero
DQuery would fail
💡 Hint
Without GROUP BY, aggregate functions apply to all rows as one group.
Concept Snapshot
GROUP BY single column syntax:
SELECT column, aggregate_function()
FROM table
GROUP BY column;
Groups rows by unique values in one column.
Aggregates apply per group.
Useful to summarize data by categories.
Full Transcript
The GROUP BY single column concept groups rows from a table by unique values in one column. The query scans each row, identifies the group key from the chosen column, and collects rows into groups. Aggregate functions like COUNT calculate values per group. For example, counting employees per department groups rows by department and counts members in each group. The execution table shows each row processed, groups formed, and counts updated. Beginners often wonder why counts increase for existing groups instead of creating new ones; this happens because rows with the same group key belong to the same group. Another point is when a new group is created for a previously unseen value. Without GROUP BY, aggregate functions apply to the entire table as one group, not per category. This visual trace helps understand how GROUP BY organizes data step-by-step.