Sobes.tech
Junior — Middle

What is the purpose of the HAVING operator in SQL queries?

sobes.tech AI

Answer from AI

The HAVING clause in SQL is used to filter groups of data formed with GROUP BY. Unlike WHERE, which filters individual rows before grouping, HAVING is applied after grouping and allows conditions on aggregate functions (e.g., COUNT, SUM, AVG).

Example:

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

This query selects only those departments with more than 10 employees.