Middle+
Why is the HAVING clause needed in SQL?
sobes.tech AI
Answer from AI
The word HAVING in SQL is used to filter groups of records obtained with the GROUP BY operator. Unlike WHERE, which filters individual rows before grouping, HAVING is applied after grouping and allows setting conditions on aggregate functions.
Example:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
This query selects departments where the number of employees is greater than 10.