Problem Statement
You need customers who placed more than 5 orders in 2025. Where should the date filter go and where should the count filter go?
Explanation
WHERE filters individual rows before grouping. Put row-level criteria like a date range there so the aggregation only sees relevant rows.
HAVING filters groups after GROUP BY. Use it for predicates on aggregates like COUNT, SUM, or AVG.
Code Solution
SolutionRead Only
SELECT customer_id, COUNT(*) AS orders_2025 FROM orders WHERE order_date >= '2025-01-01' AND order_date < '2026-01-01' GROUP BY customer_id HAVING COUNT(*) > 5;
Practice Sets
This question appears in the following practice sets:
