Sobes.tech
Junior — Middle

Can the HAVING operator be used together with the GROUP BY operator in SQL queries?

sobes.tech AI

Answer from AI

Yes, the HAVING clause is used together with GROUP BY in SQL queries to filter grouped data. While WHERE filters rows before grouping, HAVING filters groups after the GROUP BY operation.

Example:

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

This query selects departments with more than 5 employees.