Sobes.tech
Junior

What is the purpose of the HAVING operator in SQL?

sobes.tech AI

Answer from AI

The HAVING operator is used to filter the results of groups created with the GROUP BY clause. It works similarly to WHERE but applies to aggregate functions within groups.

Example: select departments where the average salary exceeds 50,000.

SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;

The key difference from WHERE is that WHERE filters rows before grouping, while HAVING filters after.