Bird
0
0

You want to find the total sales quantity for each product in each region from the sales table with columns product_id, region, and quantity. Which query correctly groups and sums the data?

hard📝 Application Q15 of 15
PostgreSQL - Aggregate Functions and GROUP BY
You want to find the total sales quantity for each product in each region from the sales table with columns product_id, region, and quantity. Which query correctly groups and sums the data?
ASELECT product_id, SUM(quantity) FROM sales GROUP BY product_id;
BSELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id;
CSELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region;
DSELECT product_id, region, SUM(quantity) FROM sales;
Step-by-Step Solution
Solution:
  1. Step 1: Identify grouping needs

    We want totals per product and per region, so both columns must be grouped.
  2. Step 2: Check each option's GROUP BY clause

    SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region; groups by product_id and region, matching SELECT columns; others miss region or GROUP BY entirely.
  3. Final Answer:

    SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region; -> Option C
  4. Quick Check:

    Group by all non-aggregated columns for correct totals [OK]
Quick Trick: Group by all selected non-aggregated columns [OK]
Common Mistakes:
  • Grouping by only one column when selecting multiple
  • Omitting GROUP BY causing aggregation errors
  • Selecting columns without aggregation or grouping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes