ROLLUP operator is used to generate subtotals and grand total of the result set. The grand total and the subtotals are generated based on the columns included in the GROUP BY clause. Based on the order of columns specified in the GROUP BY clause, ROLLUP operator creates subtotals from right to left through the list of grouping columns and then finally creates a grand total.
SYNTAX:
SELECT Column1, Column2, Aggregate_Function(Column3)
FROM Table1
GROUP BY Column1, Column2
WITH ROLLUP;
Understanding ROLLUP
Consider a Movie table as shown below.
Query :
SELECT *
FROM Movie
Output :
ID PurchaseType Category SubCategory Quantity
1 Purchase Action Action-Comedy 100
2 Purchase Action Superheroes 100
3 Purchase Drama Classics 200
4 Purchase Drama Romantic 200
5 Rental Action Action-Comedy 500
6 Rental Action Superheroes 1000
7 Rental Drama Classics 500
8 Rental Drama Romantic 1500
From the above table, if we want to summarize the data by purchase type and category, the query might look like this:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Now in the above result, if we also want to see total number of movies by purchase type, we might update the query to:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
UNION ALL
SELECT PurchaseType, NULL, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Purchase NULL 600
Rental NULL 3500
Now if we also want to see the overall total number of movies, we might update the query to:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
UNION ALL
SELECT PurchaseType, NULL, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType
UNION ALL
SELECT NULL, NULL, SUM(Quantity) AS [SUM]
FROM Movie
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Purchase NULL 600
Rental NULL 3500
NULL NULL 4100
As you can see, the code is not super readable and is unnecessarily too long. A better way of getting the same desired result is by using the ROLLUP operator as shown below.
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
WITH ROLLUP;
Output :
PurchaseType Category SUM
Purchase Action 200
Purchase Drama 400
Purchase NULL 600
Rental Action 1500
Rental Drama 2000
Rental NULL 3500
NULL NULL 4100
In SQL, a cube is a data structure that has the capability of representing data across multiple dimensions. Just like ROLLUP operator, CUBE operator will generate subtotals and grand total of the result set based on the columns included in the GROUP BY clause, but instead of creating subtotals in a hierarchical dimension, CUBE creates subtotals for all possible combinations of columns. In short, CUBE should be used if subtotal needs to be in the result for more than one column.
SYNTAX:
SELECT Column1, Column2, Aggregate_Function(Column3)
FROM Table1
GROUP BY Column1, Column2
WITH CUBE;
Understanding CUBE
Consider a Movie table as shown below.
Query :
SELECT *
FROM Movie
Output :
ID PurchaseType Category SubCategory Quantity
1 Purchase Action Action-Comedy 100
2 Purchase Action Superheroes 100
3 Purchase Drama Classics 200
4 Purchase Drama Romantic 200
5 Rental Action Action-Comedy 500
6 Rental Action Superheroes 1000
7 Rental Drama Classics 500
8 Rental Drama Romantic 1500
From the above table, if we want summarize the data by purchase type and category, the query might look like this:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Now in the above result, if we also want to see total number of movies by purchase type, we might update the query to:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
UNION ALL
SELECT PurchaseType, NULL, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Purchase NULL 600
Rental NULL 3500
Now, if we also want to see total number of movies by category in the above result, we might update the query to:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
UNION ALL
SELECT PurchaseType, NULL, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType
UNION ALL
SELECT NULL, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY Category
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Purchase NULL 600
Rental NULL 3500
NULL Action 1700
NULL Drama 2400
Now, if we also want add the grand total of movies to the result, we might update the query to:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
UNION ALL
SELECT PurchaseType, NULL, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType
UNION ALL
SELECT NULL, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY Category
UNION ALL
SELECT NULL, NULL, SUM(Quantity) AS [SUM]
FROM Movie
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
Purchase Drama 400
Rental Drama 2000
Purchase NULL 600
Rental NULL 3500
NULL Action 1700
NULL Drama 2400
NULL NULL 4100
An alternative way to get the same result is by using CUBE operator as shown below:
Query :
SELECT PurchaseType, Category, SUM(Quantity) AS [SUM]
FROM Movie
GROUP BY PurchaseType, Category
WITH CUBE;
Output :
PurchaseType Category SUM
Purchase Action 200
Rental Action 1500
NULL Action 1700
Purchase Drama 400
Rental Drama 2000
NULL Drama 2400
NULL NULL 4100
Purchase NULL 600
Rental NULL 3500
If you compare the above result with the result we got using ROLLUP operator, you would notice that by using CUBE operator, we are able to include subtotals for movie category as well.
As you can see, CUBE creates subtotals for all possible combinations. It is ideal to use CUBE when the columns represent multi-dimensional data. For example, if we need to calculate subtotals for combinations of product, year and state; CUBE would be an ideal choice. But in contrast using CUBE to calculate subtotals for day, month and year would be unnecessary for most analysis.