Bird
0
0

Which SQL query correctly groups data by columns category and year?

easy📝 Syntax Q3 of 15
SQL - GROUP BY and HAVING
Which SQL query correctly groups data by columns category and year?
ASELECT category, year, COUNT(*) FROM sales GROUP BY year;
BSELECT category, year, COUNT(*) FROM sales GROUP BY category, year;
CSELECT category, year, COUNT(*) FROM sales GROUP BY category;
DSELECT category, year, COUNT(*) FROM sales ORDER BY category, year;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct GROUP BY syntax

    The query must group by both category and year to aggregate correctly.
  2. Step 2: Check each option

    SELECT category, year, COUNT(*) FROM sales GROUP BY category, year; groups by both columns, matching the SELECT list. Options A and B group by only one column, causing errors or wrong results. SELECT category, year, COUNT(*) FROM sales ORDER BY category, year; uses ORDER BY, not GROUP BY.
  3. Final Answer:

    SELECT category, year, COUNT(*) FROM sales GROUP BY category, year; -> Option B
  4. Quick Check:

    GROUP BY columns must match SELECT columns for aggregation [OK]
Quick Trick: GROUP BY columns must match SELECT columns with aggregates [OK]
Common Mistakes:
MISTAKES
  • Grouping by fewer columns than selected
  • Using ORDER BY instead of GROUP BY
  • Missing commas in GROUP BY clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes