Bird
0
0

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📝 Application Q15 of 15
PostgreSQL - Aggregate Functions and GROUP BY
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?
AGROUP BY GROUPING SETS ((product), (region), (product, region))
BGROUP BY product, region
CGROUP BY GROUPING SETS (product, region)
DGROUP BY CUBE (product, region)
Step-by-Step Solution
Solution:
  1. Step 1: Understand required groupings

    We need three groupings: by product alone, by region alone, and by both together.
  2. Step 2: Match with GROUPING SETS syntax

    GROUPING SETS ((product), (region), (product, region)) explicitly lists all three groupings.
  3. 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.
  4. Final Answer:

    GROUP BY GROUPING SETS ((product), (region), (product, region)) -> Option A
  5. Quick Check:

    Explicit sets list all needed groupings [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes