- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL GROUPING SETS
GROUPING SETS is an extension of the GROUP BY clause that allows multiple groupings in a single query. Unlike ROLLUP and CUBE, which generate all possible subtotals, GROUPING SETS lets you specify custom groupings, making it more efficient for complex reports.
Syntax of SQL GROUPING SETS
SELECT column_name1, column_name2, aggregate_function(column_name)
FROM table_name
GROUP BY GROUPING SETS ( (column_name1), (column_name2), (column_name1, column_name2), () );
- column_name1, column_name2: The columns used for grouping.
- aggregate_function(): Functions like
SUM(),COUNT(),AVG(),MAX(), orMIN(). - GROUPING SETS(): Defines specific grouping combinations.
- () (empty set): Represents the grand total.
Key Features of SQL GROUPING SETS
- Allows custom grouping combinations instead of automatic ones.
- More efficient than
CUBEandROLLUPwhen not all subtotals are needed. - Reduces unnecessary computations in reports.
- Useful for multi-dimensional reports.
Example of SQL GROUPING SETS
Consider the following table:
Table: Sales
Query: Compute total sales for each category, each product, and the grand total
SELECT category, product, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY GROUPING SETS (
(category), -- Total sales per category
(product), -- Total sales per product
(category, product), -- Sales per category and product
() -- Grand total
);
Output:
| category | product | total_sales |
|---|---|---|
| Electronics | Laptop | 1000 |
| Electronics | Phone | 700 |
| Electronics | NULL | 1700 |
| Furniture | Chair | 500 |
| Furniture | Table | 800 |
| Furniture | NULL | 1300 |
| NULL | Laptop | 1000 |
| NULL | Phone | 700 |
| NULL | Chair | 500 |
| NULL | Table | 800 |
| NULL | NULL | 3000 |
Explanation
- GROUP BY GROUPING SETS (category): Computes total sales per category.
- GROUP BY GROUPING SETS (product): Computes total sales per product.
- GROUP BY GROUPING SETS (category, product): Computes sales per category and product.
- GROUP BY GROUPING SETS (): Computes the grand total for all rows.
SQL GROUPING SETS vs. SQL CUBE vs. SQL ROLLUP
| Feature | GROUPING SETS | CUBE | ROLLUP |
|---|---|---|---|
| Custom grouping | β | β | β |
| Generates all possible subtotals | β | β | β |
| Generates grand total | β | β | β |
| Best for large reports | β | β | β |
CUBE: Generates all possible grouping combinations.ROLLUP: Generates a hierarchical grouping (e.g., category β product β total).GROUPING SETS: Allows specific custom grouping combinations.
Example with COUNT()
Query: Count the number of products in each category and overall
SELECT category, product, COUNT(*) AS product_count
FROM sales
GROUP BY GROUPING SETS (
(category),
(product),
(category, product),
()
);
Summary
- GROUPING SETS allows customized subtotal calculations.
- It is more efficient than
CUBEwhen only specific subtotals are needed. NULLvalues indicate subtotal or total rows.- Use cases: Custom sales reports, financial analysis, and business intelligence.