SQL · easy
Asked in BI Developer interviews, in the SQL round.
WHERE filters rows before aggregation; HAVING filters after GROUP BY aggregation.
WHERE filters rows before they are grouped; HAVING filters groups after aggregation. So a condition on a raw column goes in WHERE, and a condition on an aggregate goes in HAVING.
SELECT customer_id, COUNT(*) AS orders_2025
FROM orders
WHERE order_date >= '2025-01-01' -- rows, before grouping
GROUP BY customer_id
HAVING COUNT(*) >= 5; -- groups, after aggregation
The point worth making: WHERE also does less work. Filtering in WHERE shrinks the set before the group-by, so a condition that could go in either place belongs in WHERE. Order of evaluation is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, which is also why a column alias defined in SELECT cannot be used in WHERE.
HAVING. It runs, but it groups everything first and filters late.