ROLLUP
ROLLUP is a SQL operation that creates subtotals and grand totals in the result set. It provides a hierarchical level of data aggregation, allowing analysis of data at multi-dimensional levels. It's a part of the GROUP BY clause and aggregates data from the most detailed level to a grand total in the output.
Example
Output
Explanation
In the example, the ROLLUP
operator is used to produce a result set that is similar to the one generated by the GROUP BY
clause. However, with ROLLUP
, extra rows are added to calculate the subtotals, which are grouped by Country
and State
. Also, a grand total row is added to the result set.
Example
Output
region | dept | total_salary |
---|---|---|
East | HR | 50000 |
East | IT | 80000 |
East | NULL | 130000 |
West | HR | 45000 |
West | IT | 75000 |
West | NULL | 120000 |
NULL | NULL | 250000 |
Explanation
In the provided sample, ROLLUP
operator is used to create subtotals and grand totals in the result set. It generates a result set that is similar to the one generated by the GROUP BY
clause, but with additional rows that represent subtotals and grand totals. Here, region and dept are the grouping columns. The ROLLUP
operator creates a sub-total for each unique region and a grand total row at the end.
Example
Output
Explanation
The ROLLUP operator is used to generate a result set that is similar to the one generated by the GROUP BY clause, but with additional rows that represent subtotals and grand totals. The NULL values in the result set represent subtotals and grand totals. In this specific example, the ROLLUP operator generates a total sales for each State under each Region, a subtotal sales for each Region (NULL in State column), and a grand total sales (NULL in both Region and State columns).
Example
Output
Explanation
The ROLLUP operator is used to create subtotals and grand totals in the output of a SQL query. In this example, ROLLUP creates subtotals for each ‘location’ (i.e., total sales for all departments at each location) and a grand total for all locations and departments. A NULL value in ‘department’ represents the total sales for a location across all departments. A NULL value in both ‘location’ and ‘department’ represents the grand total sales for all locations and departments.