All three aggregates return one row with three summary values: count, max, and min.
Final Answer:
Number of rows, highest amount, lowest amount -> Option A
Quick Check:
COUNT, MAX, MIN = count, max, min [OK]
Hint: Each aggregate returns one summary value in the result [OK]
Common Mistakes:
Confusing COUNT(*) with SUM(amount)
Expecting multiple rows instead of one
Thinking multiple aggregates cause syntax error
4. Identify the error in this SQL query:
SELECT SUM(price), AVG(price) FROM sales GROUP BY category;
medium
A. No error, query is correct
B. Cannot use SUM and AVG together
C. Missing GROUP BY column in SELECT clause
D. GROUP BY should be after WHERE clause
Solution
Step 1: Check GROUP BY usage
When using GROUP BY category, category must appear in SELECT to show groups.
Step 2: Identify missing column
Query selects only aggregates but misses category column, causing error or unexpected output.
Final Answer:
Missing GROUP BY column in SELECT clause -> Option C
Quick Check:
GROUP BY columns must appear in SELECT [OK]
Hint: Include GROUP BY columns in SELECT list [OK]
Common Mistakes:
Omitting GROUP BY column in SELECT
Thinking SUM and AVG can't be combined
Misplacing GROUP BY clause
5. 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
A. SELECT category, SUM(amount) AND AVG(amount) AND COUNT(*) FROM sales GROUP BY category;
B. SELECT SUM(amount), AVG(amount), COUNT(*) FROM sales;
C. SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales;
D. SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category;
Solution
Step 1: Include category in SELECT and GROUP BY
To get aggregates per category, category must be in SELECT and GROUP BY.
Step 2: Combine aggregates with commas
SUM(amount), AVG(amount), COUNT(*) are combined with commas to get totals, averages, and counts.
Final Answer:
SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category; -> Option D
Quick Check:
GROUP BY category with aggregates = SELECT category, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY category; [OK]
Hint: Group by category and list aggregates with commas [OK]