Junior — Middle
What is the role of the HAVING operator in working with groupings and data aggregation?
sobes.tech AI
Answer from AI
The HAVING operator is used in SQL to filter groups of data after applying aggregate functions (such as COUNT, SUM, AVG). Unlike WHERE, which filters rows before grouping, HAVING filters already grouped data.
For example, if you want to select groups with more than 5 records:
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
Here, HAVING allows filtering only those departments where there are more than 5 employees. This is important for analyzing aggregated data.