Bird
0
0

Given the table transactions(customer_id, product_id, total), what will be the result of this query?

medium📝 query result Q4 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Given the table transactions(customer_id, product_id, total), what will be the result of this query?
SELECT customer_id, product_id, SUM(total) FROM transactions GROUP BY GROUPING SETS ((customer_id), (product_id));

Choose the correct description of the output.
AIt returns total sales grouped by customer_id, product_id, and their combination.
BIt returns total sales grouped by both customer_id and product_id combined only.
CIt returns total sales grouped by customer_id and total sales grouped by product_id separately.
DIt returns total sales without any grouping.
Step-by-Step Solution
Solution:
  1. Step 1: Understand GROUPING SETS behavior

    GROUPING SETS ((customer_id), (product_id)) produces two grouping sets: one by customer_id and one by product_id.
  2. Step 2: Analyze output

    The query returns sums grouped separately by customer_id and by product_id, not combined.
  3. Final Answer:

    It returns total sales grouped by customer_id and total sales grouped by product_id separately. -> Option C
  4. Quick Check:

    GROUPING SETS creates multiple grouping sets, not combined unless specified [OK]
Quick Trick: GROUPING SETS lists separate groupings, not combined [OK]
Common Mistakes:
  • Assuming GROUPING SETS combines all columns in one group
  • Confusing GROUPING SETS with ROLLUP or CUBE
  • Expecting combined grouping without specifying it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes