Content
HAVING filters groups created by GROUP BY. Use it to filter aggregated results.
HAVING vs WHERE
WHERE: Filters rows BEFORE grouping
HAVING: Filters groups AFTER grouping
Basic HAVING
SELECT category, COUNT(*) as count
FROM products
GROUP BY category
HAVING COUNT(*) > 10;
HAVING with SUM
SELECT customer_id, SUM(total) as total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(total) > 1000;
HAVING with AVG
SELECT department, AVG(salary) as avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
WHERE and HAVING Together
SELECT category, AVG(price) as avg_price
FROM products
WHERE status = 'active'
GROUP BY category
HAVING AVG(price) > 50;
Multiple HAVING Conditions
SELECT category, COUNT(*), AVG(price)
FROM products
GROUP BY category
HAVING COUNT(*) > 5 AND AVG(price) < 100;
Generate HAVING Queries
AI2sql adds HAVING when you filter aggregates.


