Updated March 13, 2023
Introduction to ROLLUP in SQL
SQL ROLLUP is a subclause or extension of the GROUP BY clause, that makes it possible to aggregate values and prepare summary reports on multiple axes and provides a more detailed analysis by creating multiple grouping sets along the hierarchy of columns while using just a single query.
SQL ROLLUP is more or less like SQL CUBE metadata as both of them summarize data values along with multiple grouping sets, but they differ on the point that CUBE creates all possible groups of the data whereas ROLL UP creates grouping sets along an assumed hierarchy of data columns. Hence, we have less number of groups when working with ROLLUP than when using CUBE.
Syntax
The basic syntax for creating a SQL ROLLUP is as follows:
SELECT aggregate_function {MIN,MAX,SUM,COUNT,AVG}(column_name),
column_name1, column_name2, column_name3,...
FROM table_name
GROUP BY
ROLLUP(column_name1,column_name2,column_name3,...);
Parameters
- SELECT: It is used to select the required data from the database.
- Aggregate_function {MIN,MAX,SUM,COUNT,AVG}: Specify the aggregate function that will be used for summarization of data in the columns.
- column_name: Specify the column name on which aggregate operation will be performed.
- column_name1, column_name2, column_name3: Specify the column names along which the group sets will be made.
- FROM: It is used to specify the source from which data has to be fetched.
- GROUP BY: It is used to group rows, having similar values into summary rows.
- ROLLUP (column_name1,column_name2,column_name3): It is used to group data along multiple axes. Specify the same column names in a hierarchical manner, since ROLLUP will form groups in a hierarchical manner.
Of the above mentioned parameters, all the parameters are mandatory. You may use JOINS, WHERE, ORDER BY AND HAVING clauses based on your requirement.
Going ahead we will be discussing the above-mentioned SQL ROLLUP sub-clause in great detail.
SQL ROLLUP Sub-Clause
In order to understand the concept better, we will take the help of three tables, employees (contains personal details of all the employees), department ( contains details like department id, name, and its hod) and tasks (contains details and status of projects).
The data in the “department” table look something like this:
The data in the “employees” table is as follows :
The data in the “tasks” table look something like this :
Examples to Implement ROLLUP in SQL
Here are a few examples to understand SQL ROLLUP in great detail.
Example #1
SQL query to illustrate the difference between a simple GROUP BY, a ROLLUP and CUBE operation.
Case #1
Code:
select gender, SUM(salary)
From employees
GROUP BY gender;
Output:
Case #2
Code:
select gender, SUM(salary)
From employees
GROUP BY ROLLUP(gender);
Output:
In the first case, we are simply using a simple GROUP BY clause to summarize employee salaries along with the gender dimensions.
In the second case, we will be using the ROLLUP subclass along with the GROUP BY clause to perform the same task. In the second case we can notice an additional row showing an additional unnamed group, showing the total salary of the employees irrespective of their genders.
Example #2
SQL query to illustrate the difference between GROUP BY with a ROLLUP and GROUP BY with a CUBE operation.
Case #1 – GROUP BY with ROLLUP
Code:
select gender,departmentid, SUM(salary) as 'total_salary_rollup'
From employees
GROUP BY ROLLUP(gender,departmentid);
Output:
Case #2 – GROUP BY with CUBE
Code:
select gender,departmentid, SUM(salary) as 'total_salary_cube'
From employees
GROUP BY CUBE(gender,departmentid);
Output:
In the above example, we can see that CUBE gives us all possible groups whereas ROLLUP gives only the hierarchically possible groups.
Example #3
Summarize the salaries of employees along with each department and gender.
Case #1
Code:
SELECT departmentid, gender, SUM(salary)
From employees
GROUP BY ROLLUP(departmentid,gender);
Output:
Let’s rename the NULL values using the CASE and GROUPING function in the above example. I am using SQL SERVER for all these examples, so syntax might vary a little bit for MYSQL and PostgreSQL.
Case #2
Code:
SELECT departmentid AS 'Department', gender AS 'Gender', SUM(salary)
AS 'Total' FROM employees GROUP BY ROLLUP(departmentid,gender);
Output:
Example #4
Summarize the salary of employees along with each city, department, and gender.
Code:
SELECT departmentid AS 'Department', gender AS 'Gender', city AS 'Location', SUM(salary) AS 'Total'
FROM employees GROUP BY ROLLUP(city,departmentid,gender);
Output:
Example #5
Summarize the number of employees along with each city and department, who are currently working on a project.
Code:
SELECT e.departmentid AS 'Department', e.city AS 'Location', COUNT(e.employeeid) as 'No of employees'
FROM employees as e INNER JOIN tasks as t
ON e.employeeid = t.employeeid
GROUP BY ROLLUP(e.city,e.departmentid);
Output:
Conclusion
SQL ROLLUP is an extension of GROUP BY which is used to group data along a hierarchy of columns. It is very helpful in summarizing data along multiple axes. Hence, helps to fasten up data analysis and reporting.
Recommended Articles
We hope that this EDUCBA information on “ROLLUP in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.