WHERE for rows, HAVING for groups
-- WHERE: filter rows before groupingSELECT product_type, AVG(price) AS avg_priceFROM productsWHERE price > 100GROUP BY product_type; -- HAVING: filter groups after aggregationSELECT customer_id, SUM(total) AS total_spentFROM ordersGROUP BY customer_idHAVING SUM(total) > 1000;- Remember: a condition on a raw column belongs in WHERE
- a condition on SUM, COUNT or AVG belongs in HAVING