EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial ROLLUP in SQL
 

ROLLUP in SQL

Priya Pedamkar
Article byPriya Pedamkar

Updated March 13, 2023

rollup in sql

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

Note: A SQL ROLLUP performs grouping along an assumed hierarchy of data columns.

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:

department

The data in the “employees” table is as follows :

employees

The data in the “tasks” table look something like this :

tasks

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:

GROUP BY, a ROLLUP and CUBE

Case #2

Code:

select gender, SUM(salary)
From employees
GROUP BY ROLLUP(gender);

Output:

ROLLUP in SQL - 5

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:

ROLLUP in SQL - 6

Case #2 – GROUP BY with CUBE

Code:

select gender,departmentid, SUM(salary) as 'total_salary_cube'
From employees
GROUP BY CUBE(gender,departmentid);

Output:

ROLLUP in SQL - 7

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:

department and gender

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:

ROLLUP in SQL - 9

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:

city, department, and gender

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:

ROLLUP in SQL - 11

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.

  1. SQL Delete Join
  2. SQL Inner Join
  3. TOP in SQL
  4. SQL Outer Join
  5. Guide to PostgreSQL ROLLUP

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW