Junior — Middle
Can you explain the purpose and use of the HAVING clause in SQL queries?
sobes.tech AI
Answer from AI
The HAVING clause in SQL is used to filter results after grouping data with GROUP BY. While WHERE filters rows before grouping, HAVING filters the grouped aggregates.
Example:
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
This query selects departments with more than 10 employees. HAVING allows you to specify conditions on aggregate functions such as COUNT, SUM, AVG, etc.