1.What is the difference between WHERE and HAVING?
Show hint
Think about when each one runs relative to GROUP BY.
Show solution
WHERE filters individual rows before they are grouped. HAVING filters groups after GROUP BY and aggregate functions have been calculated.
SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE city <> 'Delhi' -- removes rows
GROUP BY department
HAVING AVG(salary) > 50000; -- removes groupsBecause of this order, you cannot use an aggregate like COUNT(*) in WHERE.