Sobes.tech
Junior — Middle

In what cases is the HAVING operator used in SQL queries?

sobes.tech AI

Answer from AI

The HAVING operator in SQL is used to filter groups of records formed with the GROUP BY clause. Unlike WHERE, which filters individual rows before grouping, HAVING filters already aggregated groups based on conditions, such as sum, count, or average.

Example:

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

This query selects departments with more than 10 employees.