- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL ROLLUP
ROLLUP is a powerful extension of the GROUP BY clause that allows you to generate multiple levels of grouping within a single query. While a standard GROUP BY produces a single set of aggregates, ROLLUP goes further by automatically generating subtotals and a grand total.
In the world of data analysis and business intelligence, ROLLUP is a "heavy lifter." It enables you to create hierarchical reports—like sales by year, then month, then day—without needing to write complex UNION ALL statements to stitch different summary levels together.
Syntax of SQL ROLLUP
SELECT column_name1, column_name2, aggregate_function(column_name)
FROM table_name
GROUP BY ROLLUP(column_name1, column_name2);
- column_name1, column_name2: These are the columns you want to group by. The order here is critical because ROLLUP creates a hierarchy from left to right.
- aggregate_function(): Standard math functions like
SUM(),COUNT(),AVG(),MAX(), orMIN(). - ROLLUP(): The specific keyword that tells the database engine to calculate subtotals for every level of the hierarchy specified in the parentheses.
SELECT statement. This ensures you can actually see which subtotal belongs to which category in your result set.
Key Features of SQL ROLLUP
- Hierarchical Aggregation: It calculates aggregates moving from the most detailed level (right-most column) up to the grand total.
- Grand Total Generation: It automatically appends a final row representing the sum of all data in the result set.
- Reduced Code Complexity: It replaces the need for multiple
GROUP BYqueries joined byUNIONoperators, which is both cleaner to read and more efficient for the database to execute. - Column Sensitivity: The order of columns inside
ROLLUP(A, B)produces different results thanROLLUP(B, A).
CUBE instead.
Example of SQL ROLLUP
Consider the following table:
Table: Sales
Query: Compute total sales for each category and product, including subtotals
SELECT category, product, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY ROLLUP(category, product);
NULL values in the result set don't always mean "missing data." In a ROLLUP, a NULL in a grouping column usually signifies a subtotal or a grand total.
Output:
| category | product | total_sales |
|---|---|---|
| Electronics | Laptop | 1000 |
| Electronics | Phone | 700 |
| Electronics | NULL | 1700 |
| Furniture | Chair | 500 |
| Furniture | Table | 800 |
| Furniture | NULL | 1300 |
| NULL | NULL | 3000 |
Explanation:
- Individual Totals: The first two rows for each category show the standard
SUMfor each specific product. - Category Subtotal: Look at row 3. The
productisNULL, and thetotal_salesis 1700. This is the sum of Laptop (1000) and Phone (700) within the "Electronics" category. - Grand Total: The very last row has
NULLfor bothcategoryandproduct. This represents the absolute total of all sales across all categories (1700 + 1300 = 3000).
COALESCE() function to make your reports more readable by replacing those NULL labels with "All Products" or "Grand Total." For example: SELECT COALESCE(category, 'All Categories') ...
SQL ROLLUP vs. GROUPING SETS vs. CUBE
Choosing the right grouping tool depends on the structure of your report. Here is a quick breakdown of how they compare:
| Feature | ROLLUP | CUBE | GROUPING SETS |
|---|---|---|---|
| Generates subtotals | ✅ | ✅ | ✅ |
| Generates grand total | ✅ | ✅ | ✅ |
| Generates all possible combinations | ❌ | ✅ | ✅ (customizable) |
| Best for hierarchical data | ✅ | ❌ | ✅ |
Use ROLLUP when your data has a natural hierarchy (e.g., Country > State > City). Use CUBE when you need to see every possible cross-section of data (e.g., sales by product regardless of region, AND sales by region regardless of product).
SQL ROLLUP with COUNT()
While SUM() is the most common use case, COUNT() is frequently used in inventory and HR reporting to see how items or employees are distributed across departments.
Example: Count the number of products in each category and overall
SELECT category, product, COUNT(*) AS product_count
FROM sales
GROUP BY ROLLUP(category, product);
ROLLUP with COUNT(), be careful if your underlying data has actual NULL values. COUNT(*) includes all rows, while COUNT(column_name) only counts non-null entries.
Summary
- ROLLUP is the go-to tool for hierarchical reports that require subtotals and grand totals in a single result set.
- The order of columns in the ROLLUP clause defines the hierarchy of the subtotals.
- The
NULLvalues produced in the grouping columns identify the summary rows. - Use cases: Perfect for financial balance sheets, sales performance dashboards, and organizational headcounts.