SQL ROLLUP
The ROLLUP operator in SQL is an extension of the GROUP BY clause. It enables hierarchical or cumulative subtotals and a grand total in the result set. It's often used for generating summary reports with multiple levels of aggregation.
Why Use SQL ROLLUP?
- Quickly compute subtotals and totals for hierarchical groupings.
- Simplifies complex aggregation queries.
- Ideal for reports and data summaries that require grouped and cumulative views.
Syntax
Key Points
- Hierarchical Aggregation:- Aggregates at each level of the specified columns, starting from the most detailed to the overall total.
 
- NULLs Indicate Totals:- Rows with NULLvalues represent subtotals or grand totals.
 
- Rows with 
- Fewer Combinations than CUBE:- Unlike CUBE,ROLLUPdoes not calculate all combinations—just cumulative groupings.
 
- Unlike 
Example Dataset
Sales Table
| Region | Product | Sales | 
|---|---|---|
| North | A | 100 | 
| North | B | 200 | 
| South | A | 150 | 
| South | B | 250 | 
Examples
1. Using ROLLUP for Region and Product
Result:
| Region | Product | TotalSales | 
|---|---|---|
| North | A | 100 | 
| North | B | 200 | 
| North | NULL | 300 | 
| South | A | 150 | 
| South | B | 250 | 
| South | NULL | 400 | 
| NULL | NULL | 700 | 
2. Using GROUPING Function
To distinguish subtotals and totals from regular rows, use the GROUPING() function. It returns 1 for grouped columns and 0 for detailed rows.
Result:
| Region | Product | TotalSales | IsRegionGrouped | IsProductGrouped | 
|---|---|---|---|---|
| North | A | 100 | 0 | 0 | 
| North | B | 200 | 0 | 0 | 
| North | NULL | 300 | 0 | 1 | 
| South | A | 150 | 0 | 0 | 
| South | B | 250 | 0 | 0 | 
| South | NULL | 400 | 0 | 1 | 
| NULL | NULL | 700 | 1 | 1 | 
3. Filtering Subtotals and Totals
You can filter the result set to include only specific levels of aggregation using the GROUPING() function.
Result:
| Region | Product | TotalSales | 
|---|---|---|
| North | NULL | 300 | 
| South | NULL | 400 | 
| NULL | NULL | 700 | 
How ROLLUP Works
For GROUP BY ROLLUP (Region, Product), SQL generates results for:
- (Region, Product)– Detailed grouping.
- (Region)– Subtotal for each- Region.
- ()– Grand total (no grouping).
Difference Between ROLLUP and CUBE
| Feature | ROLLUP | CUBE | 
|---|---|---|
| Aggregation Levels | Hierarchical (cumulative) | All combinations | 
| Use Case | Totals and subtotals | Full multi-dimensional analysis | 
| Complexity | Simpler | More comprehensive | 
Equivalent Query Without ROLLUP
Without ROLLUP, you would need to write multiple GROUP BY queries and combine their results using UNION ALL:
Using ROLLUP simplifies this process into a single query.
Common Use Cases
- Sales Reports:- Generate sales subtotals for regions and products, along with a grand total.
 
- Financial Summaries:- Aggregate revenue or expenses at different hierarchical levels.
 
- Inventory Reports:- Summarize stock levels by category, subcategory, and overall.
 
Performance Considerations
- Indexes: Proper indexing can improve performance when using ROLLUP.
- Data Size: Large datasets with multiple levels of aggregation may impact query performance.
- Database Support: Supported in major databases like SQL Server, PostgreSQL, Oracle, and MySQL (v8.0+).
Conclusion
The SQL ROLLUP operator is a powerful tool for generating hierarchical data summaries, making it ideal for reporting and business intelligence. Its ability to calculate subtotals and totals with minimal effort simplifies complex queries.

