Bird
0
0

Consider the query:

medium📝 query result Q5 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Consider the query:
SELECT region, product, SUM(sales) FROM sales_data GROUP BY GROUPING SETS ((region), (product), (region, product));

What does the output include?
ATotals by region, totals by product, and totals by region and product combined
BOnly totals by region and product combined
COnly totals by region
DOnly totals by product
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the GROUPING SETS clause

    It includes three grouping sets: region alone, product alone, and region with product together.
  2. Step 2: Understand output rows

    The output will have sums for each region, each product, and each region-product pair.
  3. Final Answer:

    Totals by region, totals by product, and totals by region and product combined -> Option A
  4. Quick Check:

    GROUPING SETS can combine single and multi-column groupings [OK]
Quick Trick: GROUPING SETS can mix single and combined column groups [OK]
Common Mistakes:
  • Assuming only one grouping set is returned
  • Ignoring combined grouping sets
  • Confusing with ROLLUP or CUBE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes