GROUPING SETS lets you group data in different ways in one query. It helps you get multiple summaries without writing many queries.
GROUPING SETS for multiple groupings in PostgreSQL
SELECT column1, column2, ..., aggregate_function(column) FROM table_name GROUP BY GROUPING SETS ( (grouping_column1, grouping_column2, ...), (grouping_column3, ...), ... );
Each set inside GROUPING SETS is a grouping you want to do.
You can list as many grouping sets as you want, separated by commas.
SELECT region, product, SUM(sales) FROM sales_data GROUP BY GROUPING SETS ((region), (product));
SELECT year, month, SUM(amount) FROM payments GROUP BY GROUPING SETS ((year), (year, month));
SELECT category, subcategory, COUNT(*) FROM items GROUP BY GROUPING SETS ((category), (subcategory));
This example creates a sales table, inserts some data, and then uses GROUPING SETS to get total sales by region and by product in one query.
CREATE TABLE sales ( region TEXT, product TEXT, sales_amount INT ); INSERT INTO sales VALUES ('North', 'Apples', 100), ('North', 'Oranges', 150), ('South', 'Apples', 200), ('South', 'Oranges', 100); SELECT region, product, SUM(sales_amount) AS total_sales FROM sales GROUP BY GROUPING SETS ((region), (product));
GROUPING SETS is useful to combine multiple GROUP BY queries into one.
Empty values in the output mean that column is not part of that grouping set.
You can also use ROLLUP or CUBE for automatic grouping sets, but GROUPING SETS gives you full control.
GROUPING SETS lets you group data in many ways in one query.
It saves time and makes reports easier by combining multiple groupings.
Use it when you want different summaries together without multiple queries.
Practice
GROUPING SETS in a PostgreSQL query?Solution
Step 1: Understand GROUPING SETS functionality
GROUPING SETS allows grouping by multiple sets of columns in one query, combining results.Step 2: Compare with other options
Filtering rows is done by WHERE, joining tables by JOIN, sorting by ORDER BY, not GROUPING SETS.Final Answer:
To group data by multiple combinations in a single query -> Option AQuick Check:
GROUPING SETS = multiple groupings in one query [OK]
- Confusing GROUPING SETS with WHERE filtering
- Thinking GROUPING SETS sorts data
- Mixing GROUPING SETS with JOIN operations
GROUPING SETS in a PostgreSQL query?Solution
Step 1: Identify correct GROUP BY usage
GROUPING SETS is used inside GROUP BY clause with sets of columns in parentheses.Step 2: Check other clauses
WHERE, ORDER BY, HAVING do not support GROUPING SETS syntax.Final Answer:
SELECT col1, col2 FROM table GROUP BY GROUPING SETS ((col1), (col2)); -> Option DQuick Check:
GROUPING SETS goes inside GROUP BY [OK]
- Placing GROUPING SETS in WHERE clause
- Using GROUPING SETS with ORDER BY
- Confusing HAVING with GROUP BY for GROUPING SETS
sales(product, region, amount), what will be the output of this query?SELECT product, region, SUM(amount) FROM sales GROUP BY GROUPING SETS ((product), (region));
Solution
Step 1: Understand GROUPING SETS ((product), (region))
This groups data twice: once by product alone, once by region alone, producing two sets of grouped sums.Step 2: Analyze output rows
Result will have rows with product and NULL region, and rows with region and NULL product, each showing sum of amounts.Final Answer:
Sum of amounts grouped by product and sum grouped by region in one result -> Option CQuick Check:
GROUPING SETS gives multiple groupings combined [OK]
- Expecting grouping by both columns together
- Thinking it returns only one grouping
- Assuming syntax error without checking GROUP BY clause
SELECT product, region, SUM(amount) FROM sales GROUP BY GROUPING SETS (product, region);
Solution
Step 1: Check GROUPING SETS syntax
GROUPING SETS expects a list of grouping sets, each enclosed in parentheses, e.g., ((product), (region)).Step 2: Identify the error
The query uses (product, region) without extra parentheses, which is invalid syntax for GROUPING SETS.Final Answer:
GROUPING SETS requires each grouping to be enclosed in parentheses -> Option BQuick Check:
GROUPING SETS syntax needs nested parentheses [OK]
- Omitting parentheses around grouping sets
- Thinking SUM() is incompatible with GROUPING SETS
- Confusing GROUP BY multiple columns with GROUPING SETS syntax
product, by region, and by both product and region together. Which GROUPING SETS clause achieves this in one query?Solution
Step 1: Understand required groupings
We need three groupings: by product alone, by region alone, and by both together.Step 2: Match with GROUPING SETS syntax
GROUPING SETS ((product), (region), (product, region)) explicitly lists all three groupings.Step 3: Evaluate other options
GROUP BY product, region groups by both columns together only; C is invalid syntax; D (CUBE) includes all combinations but is not GROUPING SETS.Final Answer:
GROUP BY GROUPING SETS ((product), (region), (product, region)) -> Option AQuick Check:
Explicit sets list all needed groupings [OK]
- Using GROUP BY without GROUPING SETS for multiple groupings
- Confusing CUBE with GROUPING SETS
- Incorrect syntax without nested parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
