Bird
0
0

Given the table sales(product, region, amount), what will be the output of this query?

medium📝 query result Q13 of 15
PostgreSQL - Aggregate Functions and GROUP BY
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));
ASum of amounts grouped by product only
BSum of amounts grouped by product and region combined
CSum of amounts grouped by product and sum grouped by region in one result
DSyntax error due to wrong GROUPING SETS usage
Step-by-Step Solution
Solution:
  1. 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.
  2. 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.
  3. Final Answer:

    Sum of amounts grouped by product and sum grouped by region in one result -> Option C
  4. Quick Check:

    GROUPING SETS gives multiple groupings combined [OK]
Quick Trick: GROUPING SETS returns all specified groupings combined [OK]
Common Mistakes:
  • Expecting grouping by both columns together
  • Thinking it returns only one grouping
  • Assuming syntax error without checking GROUP BY clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes