Tpointtech
    Tutorials
    Python Technologies Python Tutorial Django Tutorial Numpy Tutorial Pandas Tutorial Tkinter Tutorial Pytorch Tutorial Flask Tutorial OpenCV Tutorial Java Technologies Java Tutorial Servlet Tutorial JSP Tutorial Spring Boot Tutorial Spring Framework Tutorial Hibernate Tutorial JavaFX Tutorial Java Web Services Tutorial .Net Framework .Net Framework Tutorial C# Tutorial ASP.Net Tutorial ADO.Net Tutorial WPF Tutorial
    AI, ML & Data Science Artificial Intelligence Tutorial Machine Learning Tutorial Data Science Tutorial Deep Learning Tutorial TensorFlow Tutorial Artificial Neural Network Tutorial Matplotlib Tutorial Python Scipy Tutorial Cloud Technology Cloud Computing Tutorial AWS Tutorial Microsoft Azure Tutorial Salesforce Tutorial GCP Tutorial B.Tech and MCA DBMS Tutorial Data Structures Tutorial Operating System Tutorial Computer Network Tutorial DAA Tutorial Computer Organization Tutorial Software Engineering Tutorial Data Mining Tutorial
    Web Technologies HTML Tutorial CSS Tutorial JavaScript Tutorial Jquery Tutorial Angular 8 Tutorial React Tutorial React Native Tutorial Node.js Tutorial PHP PHP Tutorial MySQL Tutorial Laravel Tutorial WordPress Tutorial CodeIgniter Tutorial Software Testing Software Testing Tutorial Selenium Tutorial JIRA Tutorial JMeter Tutorial Postman Tutorial TestNG Tutorial SoapUI Tutorial Cucumber Tutorial
    Interviews
    Technical Interview Python Interview Questions Java Interview Questions C Interview Questions C++ Interview Questions C# Interview Questions .NET Interview Questions HR Interview Questions Java & Backend Spring Boot Interview Questions Spring Interview Questions Hibernate Interview Questions JDBC Interview Questions Servlet Interview Questions Maven Interview Questions Jenkins Interview Questions Microservices Interview Questions Python Frameworks Django Interview Questions Pandas Interview Questions Python Coding Interview Questions Python Interview Questions for Experienced
    Web Development HTML Interview Questions CSS Interview Questions JavaScript Interview Questions jQuery Interview Questions AJAX Interview Questions React Interview Questions Angular Interview Questions Node.js Interview Questions Express.js Interview Questions Front-End Developer Interview Questions Database DBMS Interview Questions SQL Interview Questions PL/SQL Interview Questions MySQL Interview Questions MongoDB Interview Questions Redis Interview Questions Oracle Interview Questions Core CS (B.Tech / MCA) Operating System Interview Questions OOPs Interview Questions DSA Interview Questions Computer Networks Interview Questions
    Data, Cloud & Tools Power BI Interview Questions Excel Interview Questions Machine Learning Interview Questions AWS Interview Questions Software Testing Manual Testing Interview Questions Selenium Interview Questions API Testing Interview Questions ETL Testing Interview Questions Mobile Testing Interview Questions MCQs Python MCQs Java MCQs PHP MCQs C Programming MCQs C++ MCQs SQL MCQs DBMS MCQs Data Structure MCQs Software Engineering MCQs Artificial Intelligence MCQs
    Compilers
    Online Compilers Python Online Compiler Java Online Compiler PHP Online Compiler C Online Compiler C++ Online Compiler JavaScript Online Compiler TypeScript Online Compiler R Online Compiler Swift Online Compiler Kotlin Online Compiler Go Online Compiler C# Online Compiler Perl Online Compiler Groovy Online Compiler
    Online Editors HTML Online Editor HTML, CSS & JavaScript Online Editor
    • Python
    • Java
    • JavaScript
    • SQL
    • C
    • C++
    • HTML
    • CSS
    • React
    • Node.js
    • Spring Boot
    • C#
    • PHP
    • MySQL
    • MongoDB
    • AI
    • ML
    • DSA
    • DBMS
    • OS

    • SQL Server Tutorials
    • Install Visual Studio
    • Install SQL Server
    • SQL Server Management Studio
    • SQL Server Datatypes

    • SQL Server Login Database
    • SQL Server Create Database
    • SQL Server Select Database
    • SQL Server DROP Database

    • SQL Server Create Table
    • SQL Server Delete Table
    • SQL Server Insert Data
    • SQL Server Update Data
    • SQL Server Delete Data
    • SQL Server Delete TOP
    • SQL Server ALTER Table
    • View in SQL Server

    • SQL Server MIN Function
    • SQL Server MAX Function
    • SQL Server SUM Function
    • SQL Server AVG Function
    • SQL Server COUNT Function

    • SQL Server Comparison Operator
    • SQL Server UNION Operator
    • SQL Server INTERSECT Operator
    • SQL Server IN Operator
    • SQL Server NOT Operator
    • SQL Server BETWEEN Operator
    • SQL Server IS NULL Operator
    • SQL Server IS NOT NULL Operator
    • SQL Server LIKE Operator
    • SQL Server EXIST Operator

    • SQL Server DISTINCT Clause
    • SQL Server GROUP BY Clause
    • SQL Server WHERE Clause
    • SQL Server ORDER BY
    • SQL Server HAVING
    • SQL Server SELECT
    • SQL Server GROUPING SETS

    • SQL Server Primary Keys
    • SQL Server Enable Primary Key
    • SQL Server Disable Primary Key
    • SQL Server Drop Primary Key
    • SQL Server SS Foreign Keys
    • SQL Server Enable Foreign Key
    • SQL Server Disable Foreign Key
    • SQL Server Drop Foreign Key

    • SQL Server Create View
    • SQL Server Backup Database
    • SQL Server Restore Database

    • SQL Server Substring
    • CTE in SQL Server
    • Cursor in SQL Server
    • Index in SQL Server
    • SQL Server Row Number
    • Rename Columns in SQL Server
    • SQL Server CASE
    • SQL Server JOINS
    • Add Columns in SQL Server
    • Drop Column in SQL Server
    • SQL Server Describe Table
    • SQL Server Replace
    • SQL Server COALESCE
    • SQL Server IF ELSE
    • SQL Server ISNULL Function
    • SQL Server Temp Table
    • Stored Procedure in SQL Server
    • Triggers in SQL Server
    • SQL Server CAST
    • SQL Server CONVERT
    • SQL Server SELECT INTO
    • SQL Server Aggregate Functions
    • SQL Server INSERT INTO SELECT
    • SQL Server NULLIF
    • SQL Server Copy Tables
    • SQL Server Rename Table
    • SQL Server Truncate Table
    • How to find SQL Server Version
    • SQL Server CHARINDEX() Function
    • Table Variable in SQL Server
    • RANK Function in SQL Server
    • SQL Server PIVOT
    • EOMONTH Function in SQL Server
    • SQL Server Alias
    • SQL Server Left Function
    • SQL Server Sequence
    • SQL Server Synonym
    • SS DATEDIFF Function
    • SQL Server WHILE LOOP
    • Find and Delete Duplicates From a Table in SQL Server
    • SQL Server CURRENT_TIMESTAMP Function
    • SQL Server DATEADD Function
    • SQL Server MERGE
    • SQL Server OFFSET FETCH
    • Locks in SQL Server
    • SQL Server ROLLUP
    • SQL Server CROSS JOIN
    • SQL SERVER UPDATE JOIN
    • SQL Server Comments
    • SQL Server IDENTITY
    • SQL Server CONCAT Function
    • SQL Server CONCAT_WS Function
    • SQL Server Replication
    • SQL Server Transaction
    • SQL Server Constraints
    • SQL Server PROFILER
    • SQL Server Functions
    • SQL Server Window Functions
    • SQL Server Date Functions
    • SQL Server Mathematical Functions
    • SQL Server String Functions
    • SQL Server RENAME Database
    • SQL Server Show/List Databases
    • SQL Server STUFF() Function
    • SQL Server Collation
    • SQL Server Composite Key
    • SQL Server Unique Key
    • How to Determine the Service Pack Currently Installed on SQL Server

    • SSIS Tutorial
    • SSRS Tutorial

    • SQL Server Interview
    Home › SQL Server › SQL Server ROLLUP
    ← prev

    SQL Server ROLLUP

    Last Updated : 17 Mar 2025

    The ROLLUP clause in SQL Server is an extension of the grouping set operator. This article will give a complete overview of the ROLLUP clause to aggregates different groups with subtotals and grand totals.

    It is the subclass of the GROUP BY clause that performs an aggregate operation on multiple levels in a hierarchy and prepares summary reports. It allows us to generate multiple grouping sets within a single query, which is impossible with the GROUP BY clause as it aggregates a single group. Thus, we can say that the ROLLUP provides a more detailed analysis by employing a single query to create several grouping sets along the hierarchy of columns.

    NOTE: ROLLUP modifier produces the summary output, including extra rows that represent super-aggregate summary operations. It is mainly used to provide support for OLAP (Online Analytical Processing) operations.

    Syntax

    The following is the basic syntax that illustrates the ROLLUP clause in SQL Server:

    The parameters of the above syntax are described below:

    • column_lists: It indicates the name of columns from the defined table.
    • aggregate_function (column): It is used to perform aggregation on given columns such as SUM, COUNT, AVG, etc.
    • table_name: It indicates the source table name from which data will be fetched.
    • GROUP BY: It's a clause used to specify a single column or multiple columns to create a group on which the aggregate operation is performed.
    • ROLLUP: It is used with the combination of the GROUP BY clause for creating multiple groups (i.e., grouping set) and hierarchically applies the aggregate function.

    SQL Server also provides another syntax to use the ROLLUP clause as given below:

    Both the syntax will return the same output.

    SQL Server ROLLUP Example

    Let us understand when and how the ROLLUP clause is used in the query practically. We can do this by first creating a table named EMPLOYEE using the below statement:

    Next, we will insert some records into this table as below:

    Execute the SELECT statement to verify the table data:

    SQL Server ROLLUP

    In the above table, we can see that we have got employees from three different countries US, UK, and India. Now, based on these table data, we want to write a query to retrieve salaries grouped by country as below:

    Executing the query will return the following output:

    SQL Server ROLLUP

    This query is good if we want to get the total salary based on country. But it's not a better option when we want to get the grand totals of all salaries in the result also because it can only aggregate data at one level, which is subtotal, not grand totals. Hence, we will use the ROLLUP clause along with GROUP BY to examine both the subtotal and the grand totals for a category as it extends the capability of the GROUP BY clause.

    The following query is used to retrieve subtotal and grand totals of salaries grouped by country:

    We will get the below output:

    SQL Server ROLLUP

    We can also get the same output using the below syntax:

    Here is the result:

    SQL Server ROLLUP

    In the output, we can see the total salary for each country as well as returns the grand totals of all salaries. The ROLLUP clause will add an extra row in the result set to display the grand totals. Here NULL represents the grand totals. We can also give it a meaningful name using the COALESCE function to replace the NULL value with any provided text.

    The below statement will substitute "Grand Total" for the null value:

    Here is the result that replaced the NULL value with the given text:

    SQL Server ROLLUP

    ROLLUP with multiple columns

    We already know that the ROLLUP clause performs aggregate operations on multiple levels in a hierarchy and prepare summary reports at each level within a single query.

    Let's understand this concept with an example. Suppose there are multiple columns in the GROUP BY clause. In this case, the ROLLUP clause assumes a hierarchy among the columns supplied in the GROUP BY clause. The query adds an extra super-aggregate summary row to the end of the result whenever a column value changes.

    For example, we have specified the three columns in the GROUP BY clause as below:

    The ROLLUP modifier assumes the hierarchy as below:

    And generates the following grouping set:

    The following query helps to understand it more clearly:

    Since we have not used the ROLLUP clause, a summary of the employee table based on multiple columns supplied in the GROUP BY clause would look like the below output. In this case, we will only receive summary values at the gender/country level of analysis.

    SQL Server ROLLUP

    Now we will analyze the output with the ROLLUP clause:

    Executing the query will display the following output where we can see the several extra rows:

    SQL Server ROLLUP

    This output generates data at three different levels of analysis, which are described below:

    • The first level generates an extra sub-aggregate summary row for each set of female employees and then displays the total salary for all employees. It will set the country column to NULL.
    • The second level generates an extra sub-aggregate summary row for each set of male employees and then displays the total salary. It will also set the country column to NULL.
    • Finally, the third level will generate an extra super-aggregate summary row that shows the grand totals for all columns. It will set the gender and country columns to NULL.

    It ensures that we get a different answer if we modify the order of the columns defined in the GROUP BY clause. See the below query:

    Here is the result:

    SQL Server ROLLUP

    GROUPING Function with ROLLUP

    The GROUPING() function determines whether NULL in the output is a regular grouped value, a super-aggregate value, or grand totals. It yields one when NULL occurs in a supper-aggregate row. Otherwise, it returns 0. The GROUPING() function can be used in the select list, HAVING clause, and ORDER BY clause.

    See the below query:

    Execute the query will return the below output:

    SQL Server ROLLUP

    In the output, we can see that the GROUPING(gender) returns one when NULL in the gender column occurs in a super-aggregate row. Otherwise, it will return zero.

    Similarly, the GROUPING(country) returns one when NULL in the country column occurs in a super-aggregate row. Otherwise, it will return zero.

    How is ROLLUP different from CUBE?

    ROLLUP and CUBE in SQL Server are used for reporting purposes and display the subtotal and grand totals. We generally use these clauses with the GROUP BY clause. However, both are used for similar objectives, but they have some differences also.

    ROLLUP returns an output that shows aggregates for a hierarchy of values in the columns you've chosen. On the other hand, CUBE creates a result set that includes aggregates for all possible combinations of values in the columns that have been selected.

    Let us take an example to illustrate these differences. Suppose we have three columns c1, c2, and c3. Next, we will perform aggregation at a different level.

    If we use these columns with the CUBE clause as CUBE(c1, c2, c3), we will get the eight possible grouping sets:

    If we use these columns with the ROLLUP clause as ROLLUP(c1, c2, c3), we will get only four possible grouping sets:


    Next TopicSQL Server CROSS JOIN


    ← prev next →



    Related Posts

    • SQL Server Replication
    • SQL Server Rename Table
    • SQL Server Transaction
    • SQL Server Constraints
    • SQL Server IDENTITY
    • SQL Server CURRENT_TIMESTAMP Function
    • How to find SQL Server Version
    • RANK Function in SQL Server
    • SQL Server Unique Key
    • SQL Server PIVOT

    Subscribe to Tpoint Tech

    We request you to subscribe our newsletter for upcoming updates.



    Logo

    We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.

    Contact info

    G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

    hr@tpointtech.com

    +91-9599086977

    Follow us
    Tpoint Tech Facebook PageTpoint Tech X PageTpoint Tech Linkedin PageTpoint Tech Telegram ChannelTpoint Tech Youtube ChannelTpoint Tech instagram Page
    Top Tutorials
    Java Data Structures C Programming C++ C# PHP HTML JavaScript jQuery Spring
    Interview Questions
    Python Java Data Structure C++ C HTML JavaScript jQuery SQL Power BI
    Online Compilers
    C Compiler R Compiler C++ Compiler PHP Compiler Java Compiler HTML Editor Swift Compiler Python Compiler JavaScript Editor TypeScript Editor
    Latest Post | Tutorials List | Privacy Policy | About Us | Contact Us
    © Copyright TpointTech.com. All Rights Reserved.
    TPoint Tech Logo
    Advertisement