Middle+
What is the difference between WHERE and HAVING?
sobes.tech AI
Answer from AI
WHERE and HAVING are both filtering operators in SQL, but they are used at different stages of data processing.
WHEREfilters rows before grouping and aggregation. It applies to individual table records.HAVINGfilters groups obtained after applying aggregate functions (e.g.,SUM,COUNT,AVG).
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 employees
Here, WHERE filters rows before grouping, and HAVING filters after grouping by departments.