Middle
Kuidas erinevad WHERE ja HAVING?
sobes.tech AI
Vastus AI-lt
In SQL, the WHERE operator is used to filter rows before grouping, meaning it limits the dataset that will participate in the query.
HAVING is used to filter already grouped data, meaning after executing aggregate functions and grouping.
Example:
SELECT department, COUNT(*) as employee_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 such employees
Here, first WHERE filters rows with salary above 50000, then data is grouped by departments, and HAVING filters groups based on the number of employees.