0
0
PostgreSQLquery~20 mins

GROUPING SETS for multiple groupings in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUPING SETS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GROUPING SETS with two grouping sets

Consider a sales table with columns region, product, and sales_amount. What is the output of this query?

SELECT region, product, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY GROUPING SETS ((region), (product));
PostgreSQL
SELECT region, product, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY GROUPING SETS ((region), (product));
ARows grouped by region only and rows grouped by product only, with NULLs in the other column
BRows grouped by region and product together and also total sum without grouping
CRows grouped by both region and product combined only
DRows grouped by region only, product only, and also by both region and product combined
Attempts:
2 left
💡 Hint

GROUPING SETS allows you to specify multiple groupings separately.

🧠 Conceptual
intermediate
1:30remaining
Understanding GROUPING SETS behavior

Which statement best describes what GROUPING SETS ((a), (b)) does in a GROUP BY clause?

AIt groups rows by column <code>a</code> only and ignores column <code>b</code>.
BIt groups rows by the combination of columns <code>a</code> and <code>b</code> only.
CIt groups rows by column <code>a</code> and separately by column <code>b</code>, producing two sets of grouped results.
DIt groups rows by column <code>b</code> only and ignores column <code>a</code>.
Attempts:
2 left
💡 Hint

Think about how GROUPING SETS lets you specify multiple groupings.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in GROUPING SETS query

Which option contains a syntax error in the GROUP BY clause using GROUPING SETS?

SELECT department, role, COUNT(*) FROM employees
GROUP BY GROUPING SETS (department, (role));
AGROUP BY GROUPING SETS ((department), (role))
BGROUP BY GROUPING SETS (department, role)
C))elor( ,)tnemtraped(( STES GNIPUORG YB PUORG
DGROUP BY GROUPING SETS ((department, role))
Attempts:
2 left
💡 Hint

GROUPING SETS requires each grouping set to be enclosed in parentheses.

optimization
advanced
2:30remaining
Optimizing multiple groupings with GROUPING SETS

You want to get total sales grouped by region, product, and also the grand total. Which GROUP BY clause is the most efficient and correct?

AGROUP BY GROUPING SETS ((region), (product), ())
BGROUP BY region, product WITH ROLLUP
CGROUP BY region, product, ()
DGROUP BY GROUPING SETS ((region, product), ())
Attempts:
2 left
💡 Hint

Think about how to include multiple groupings and the grand total.

🔧 Debug
expert
3:00remaining
Why does this GROUPING SETS query produce unexpected NULLs?

Given the query:

SELECT region, product, SUM(sales) FROM sales_data
GROUP BY GROUPING SETS ((region, product), (region), (product));

Why might some rows have NULL in both region and product columns?

ABecause the SUM aggregation returns NULL when no rows match.
BBecause GROUPING SETS automatically adds an empty grouping set causing NULLs.
CBecause the columns <code>region</code> and <code>product</code> contain NULL values in the data.
DBecause the query is missing the empty grouping set <code>()</code> which produces the grand total row with NULLs.
Attempts:
2 left
💡 Hint

Think about what produces the grand total row with NULLs in grouping columns.