Using GROUPING SETS for Multiple Groupings in PostgreSQL
📖 Scenario: You are working as a data analyst for a retail company. The company wants to analyze sales data to understand revenue by different combinations of product categories and regions.
🎯 Goal: Build a PostgreSQL query using GROUPING SETS to get total sales grouped by category, by region, and by both category and region together.
📋 What You'll Learn
Create a table named sales with columns category (text), region (text), and amount (numeric).
Insert the exact sales data provided into the sales table.
Write a query using GROUPING SETS to group sales by category, by region, and by both category and region.
Include a column named total_sales that sums the amount for each grouping.
💡 Why This Matters
🌍 Real World
Retail companies often analyze sales data by different dimensions like product categories and regions to make better business decisions.
💼 Career
Data analysts and database developers use GROUPING SETS to efficiently generate multiple grouped summaries in a single query, saving time and resources.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns category (text), region (text), and amount (numeric). Then insert these exact rows into sales: ('Electronics', 'North', 100), ('Electronics', 'South', 150), ('Clothing', 'North', 200), ('Clothing', 'South', 250).
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO with multiple rows to add the data.
2
Define the grouping sets for multiple groupings
Create a variable or comment named grouping_sets that lists the grouping sets: (category), (region), and (category, region).
PostgreSQL
Hint
List the grouping sets exactly as (category), (region), and (category, region).
3
Write the query using GROUPING SETS
Write a SELECT query on sales that uses GROUP BY GROUPING SETS with the sets (category), (region), and (category, region). Select category, region, and sum of amount as total_sales.
PostgreSQL
Hint
Use GROUP BY GROUPING SETS with the exact grouping sets and select the sum of amount as total_sales.
4
Complete the query with ordering
Add an ORDER BY clause to the query to sort results by category ascending and then by region ascending.
PostgreSQL
Hint
Use ORDER BY category ASC, region ASC to sort the results.
Practice
(1/5)
1. What is the main purpose of using GROUPING SETS in a PostgreSQL query?
easy
A. To group data by multiple combinations in a single query
B. To filter rows based on conditions
C. To join multiple tables together
D. To sort the result set by multiple columns
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 A
Quick Check:
GROUPING SETS = multiple groupings in one query [OK]
Hint: GROUPING SETS groups multiple ways at once [OK]
Common Mistakes:
Confusing GROUPING SETS with WHERE filtering
Thinking GROUPING SETS sorts data
Mixing GROUPING SETS with JOIN operations
2. Which of the following is the correct syntax to use GROUPING SETS in a PostgreSQL query?
easy
A. SELECT col1, col2 FROM table ORDER BY GROUPING SETS (col1, col2);
B. SELECT col1, col2 FROM table WHERE GROUPING SETS (col1, col2);
C. SELECT col1, col2 FROM table HAVING GROUPING SETS (col1, col2);
D. SELECT col1, col2 FROM table GROUP BY GROUPING SETS ((col1), (col2));
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 D
Quick Check:
GROUPING SETS goes inside GROUP BY [OK]
Hint: GROUPING SETS always follows GROUP BY keyword [OK]
Common Mistakes:
Placing GROUPING SETS in WHERE clause
Using GROUPING SETS with ORDER BY
Confusing HAVING with GROUP BY for GROUPING SETS
3. Given the table 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));
medium
A. Sum of amounts grouped by product only
B. Sum of amounts grouped by product and region combined
C. Sum of amounts grouped by product and sum grouped by region in one result
Hint: Each grouping set must be in its own parentheses [OK]
Common Mistakes:
Omitting parentheses around grouping sets
Thinking SUM() is incompatible with GROUPING SETS
Confusing GROUP BY multiple columns with GROUPING SETS syntax
5. You want to create a report showing total sales grouped by product, by region, and by both product and region together. Which GROUPING SETS clause achieves this in one query?
hard
A. GROUP BY GROUPING SETS ((product), (region), (product, region))
B. GROUP BY product, region
C. GROUP BY GROUPING SETS (product, region)
D. GROUP BY CUBE (product, region)
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 A
Quick Check:
Explicit sets list all needed groupings [OK]
Hint: List all groupings inside GROUPING SETS parentheses [OK]
Common Mistakes:
Using GROUP BY without GROUPING SETS for multiple groupings
Confusing CUBE with GROUPING SETS
Incorrect syntax without nested parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.