Middle
What is the difference between WHERE and HAVING?
sobes.tech AI
Answer from AI
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, that is, after performing 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, WHERE first filters rows with a salary above 50000, then data is grouped by departments, and HAVING filters groups based on the number of employees.