Aggregates & GROUP BY · Question 13 · Medium

A table Sales(sale_id, rep, region, amount) is given. Which query lists the reps whose total sales are more than 800?

  1. SELECT rep FROM Sales WHERE SUM(amount) > 800 GROUP BY rep;
  2. SELECT rep FROM Sales WHERE amount > 800 GROUP BY rep;
  3. SELECT rep FROM Sales GROUP BY rep HAVING SUM(amount) > 800;
  4. SELECT rep, SUM(amount) FROM Sales HAVING amount > 800;
Answer and explanation

Answer: C. SELECT rep FROM Sales GROUP BY rep HAVING SUM(amount) > 800;

Explanation: A condition on a group total must go in HAVING, which runs after GROUP BY. WHERE cannot contain SUM, and WHERE amount > 800 tests single sales, not the total per rep.

Previous: Table Innings: inn_id | batter | team | runs 1 | Kunal | Tigers | 45 2 | Kunal… · Next: Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…

Practise aggregates & group by

Try a fresh set of aggregates & group by questions with an explanation after every answer, free and without signing up: Aggregates & GROUP BY practice. Or sit a full placement mock test.

More aggregates & group by questions

More sql practice

Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge