Imagine sorting a huge pile of Laundry.
WHERE (Filter): First, you throw away all the dirty socks. You don't even want to group them. You filter them OUT individually.
GROUP BY (Buckets): Now you group the clean clothes into piles: "Shirts", "Pants", "Socks".
HAVING (Filter Groups): Now you look at the piles. You say "I only want piles that have more than 5 items". You aren't checking individual shirts anymore; you are checking the size of the pile.
You keep the big piles, and ignore the small ones.
Summary: WHERE filters items. HAVING filters piles.
You can't check an average before you calculate it! That's why we need HAVING—it works on the summary data, while WHERE works on raw data.
First `GROUP BY Department` to find average salary. Then `HAVING AVG(Salary) > 5000` to show only rich departments.
"Can we use WHERE with aggregate functions (like SUM, AVG)?"
No, use HAVING instead.