Bird
0
0

You want to find the total sales, average sales, and number of sales for each product category in a sales table with columns category and amount. Which query correctly combines these aggregates?

hard📝 Application Q15 of 15
SQL - Aggregate Functions
You want to find the total sales, average sales, and number of sales for each product category in a sales table with columns category and amount. Which query correctly combines these aggregates?
ASELECT category, SUM(amount) AND AVG(amount) AND COUNT(*) FROM sales GROUP BY category;
BSELECT SUM(amount), AVG(amount), COUNT(*) FROM sales;
CSELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales;
DSELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category;
Step-by-Step Solution
Solution:
  1. Step 1: Include category in SELECT and GROUP BY

    To get aggregates per category, category must be in SELECT and GROUP BY.
  2. Step 2: Combine aggregates with commas

    SUM(amount), AVG(amount), COUNT(*) are combined with commas to get totals, averages, and counts.
  3. Final Answer:

    SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category; -> Option D
  4. Quick Check:

    GROUP BY category with aggregates = SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category; [OK]
Quick Trick: Group by category and list aggregates with commas [OK]
Common Mistakes:
MISTAKES
  • Missing GROUP BY clause
  • Using AND instead of commas between aggregates
  • Not including category in SELECT when grouping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes