Bird
0
0

You have a table sales with columns region, product, month, and amount. You want to find the total sales amount for each region and product for each month. Which query achieves this?

hard📝 Application Q8 of 15
SQL - GROUP BY and HAVING
You have a table sales with columns region, product, month, and amount. You want to find the total sales amount for each region and product for each month. Which query achieves this?
ASELECT region, product, month, SUM(amount) FROM sales GROUP BY region, product;
BSELECT region, product, month, SUM(amount) FROM sales GROUP BY month;
CSELECT region, product, month, SUM(amount) FROM sales GROUP BY region, product, month;
DSELECT region, product, month, SUM(amount) FROM sales;
Step-by-Step Solution
Solution:
  1. Step 1: Identify grouping needs

    We want totals per region, product, and month, so all three columns must be grouped.
  2. Step 2: Check query options

    SELECT region, product, month, SUM(amount) FROM sales GROUP BY region, product, month; groups by all three columns, matching the SELECT list. Options A and C group by fewer columns, causing incorrect aggregation. SELECT region, product, month, SUM(amount) FROM sales; lacks GROUP BY, so no grouping occurs.
  3. Final Answer:

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

    Group by all columns you want distinct totals for [OK]
Quick Trick: Group by all columns in SELECT except aggregates [OK]
Common Mistakes:
MISTAKES
  • Grouping by fewer columns than selected
  • Omitting GROUP BY clause
  • Grouping by unrelated columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes