SQL HAVING Clause
Definition
The SQL HAVING clause filters groups produced by GROUP BY, allowing conditions on aggregate functions like COUNT, SUM, and AVG that cannot be used in WHERE.
Introduction to SQL HAVING Clause
HAVING is the WHERE clause for grouped data. Because WHERE filters individual rows before aggregation, you cannot use it to filter on aggregate results (like WHERE COUNT(*) > 5). HAVING fills this gap — it evaluates conditions after GROUP BY aggregation, filtering which groups appear in the final result.
Syntax
SELECT column, AGG_FUNC(value) FROM table GROUP BY column HAVING AGG_FUNC(value) operator value;
Examples
Filter Departments with More Than 5 Employees
SELECT department, COUNT(*) AS headcount FROM employees GROUP BY department HAVING COUNT(*) > 5 ORDER BY headcount DESC;
Cannot use WHERE COUNT(*) > 5 (WHERE runs before GROUP BY). HAVING filters after grouping — only returns departments with more than 5 employees.
Try in PlaygroundCombine WHERE and HAVING
SELECT department, AVG(salary) AS avg_salary FROM employees WHERE status = 'Active' -- filters rows before grouping GROUP BY department HAVING AVG(salary) > 75000; -- filters groups after aggregation
WHERE and HAVING can be used together. WHERE filters active employees, GROUP BY groups them, HAVING keeps only high-earning departments.
Try in PlaygroundCommon Mistakes
Using column aliases in HAVING (not supported in many databases — repeat the aggregate expression)
Confusing WHERE and HAVING — WHERE cannot reference aggregate functions
Forgetting that HAVING requires GROUP BY (in most databases — without GROUP BY, the entire table is one group)
Putting non-aggregate conditions in HAVING instead of WHERE (correct but inefficient)
Frequently Asked Questions
What is the difference between WHERE and HAVING in SQL?
Can you use HAVING without GROUP BY?
Can HAVING reference column aliases?
Related SQL Topics
Practice This in the SQL Playground
Write real queries, see live results, and master SQL HAVING Clause hands-on. 100% free.