0
0
PostgreSQLquery~10 mins

GROUPING SETS for multiple groupings in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GROUPING SETS for multiple groupings
Start Query
FROM table
Apply GROUPING SETS
Calculate aggregates for each grouping set
Combine results
Return final result set
The query reads data from the table, applies multiple grouping sets, calculates aggregates for each set, combines all results, and returns them.
Execution Sample
PostgreSQL
SELECT department, role, COUNT(*) AS count
FROM employees
GROUP BY GROUPING SETS ((department), (role));
This query counts employees grouped by department and separately by role using GROUPING SETS.
Execution Table
StepActionGrouping SetGroup Key ValuesAggregate CalculationOutput Row
1Read row-department=Sales, role=Manager--
2Read row-department=Sales, role=Developer--
3Read row-department=HR, role=Manager--
4Apply grouping set 1(department)SalesCOUNT=2(Sales, NULL, 2)
5Apply grouping set 1(department)HRCOUNT=1(HR, NULL, 1)
6Apply grouping set 2(role)ManagerCOUNT=2(NULL, Manager, 2)
7Apply grouping set 2(role)DeveloperCOUNT=1(NULL, Developer, 1)
8Combine results---4 rows total
9Return result---Query ends
💡 All rows processed and aggregates computed for each grouping set.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 7Final
department group counts{}{"Sales":2}{"Sales":2,"HR":1}{"Sales":2,"HR":1}{"Sales":2,"HR":1}{"Sales":2,"HR":1}
role group counts{}{}{}{"Manager":2}{"Manager":2,"Developer":1}{"Manager":2,"Developer":1}
Key Moments - 2 Insights
Why do we see NULL in the role column when grouping by department?
When grouping by department only, the role column is not part of the grouping key, so it shows NULL in those rows as seen in execution_table rows 4 and 5.
How does GROUPING SETS differ from using multiple GROUP BY queries?
GROUPING SETS runs all groupings in one query and combines results, as shown in execution_table steps 4-7, instead of running separate queries for each grouping.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the grouping set applied?
A(department)
B(department, role)
C(role)
DNo grouping set applied
💡 Hint
Check the 'Grouping Set' column at step 6 in the execution_table.
At which step does the query combine all grouping set results?
AStep 8
BStep 7
CStep 4
DStep 9
💡 Hint
Look for the action 'Combine results' in the execution_table.
If we add (department, role) to GROUPING SETS, how would variable 'department group counts' change after step 7?
AIt would remain the same
BIt would include counts grouped by both department and role
CIt would reset to empty
DIt would only count roles
💡 Hint
Consider how adding a new grouping set affects counts tracked per grouping key.
Concept Snapshot
GROUPING SETS syntax:
SELECT columns, aggregate()
FROM table
GROUP BY GROUPING SETS ((group1), (group2), ...);

It runs multiple groupings in one query.
Each grouping set produces separate grouped results.
NULL appears in columns not in the current grouping set.
Full Transcript
This visual execution shows how a PostgreSQL query with GROUPING SETS works step-by-step. The query reads each row from the employees table. Then it applies each grouping set separately: first grouping by department, then by role. For each grouping set, it counts how many rows belong to each group. The results from all grouping sets are combined into one final result set. Variables track counts per group key. Nulls appear in columns not included in the current grouping set. This method is efficient to get multiple groupings in one query instead of running many queries.