GROUP BY and HAVING

Published July 26, 2025 By Sai Sree Ram Gundepudi

GROUP BY and HAVING

GROUP BY

SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id;

HAVING

SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 5000;

HAVING filters groups after aggregation, unlike WHERE which filters rows before aggregation.

Was this solution helpful?

Your feedback helps improve our technical knowledge repository.

Share this article