Sobes.tech
Senior

В чем разница между WHERE и HAVING?

sobes.tech AI

Answer from AI

In SQL, the WHERE operator filters rows before grouping, meaning it applies to individual table records. HAVING, on the other hand, filters already grouped data, so it is applied after GROUP BY.

Example:

SELECT department, COUNT(*) as emp_count
FROM employees
WHERE salary > 50000  -- filter employees with salary greater than 50000
GROUP BY department
HAVING COUNT(*) > 10    -- select departments with more than 10 employees

Thus, WHERE restricts the initial data, while HAVING filters the results of aggregate functions.