SQL GROUP BY and HAVING
MediumGROUP BY collapses rows sharing common values into summary rows; HAVING filters these groups after aggregation.
Why It Matters
GROUP BY is tested in virtually every SQL interview. Understanding the WHERE vs HAVING distinction and what can appear in SELECT alongside GROUP BY is critical.
How It Works
Example
Common Mistakes
Using WHERE with aggregate functions (WHERE COUNT(*) > 5 fails — use HAVING). Including a non-grouped, non-aggregated column in SELECT. Confusing when to use WHERE vs HAVING.
Ad Space
Test Your Understanding
Which query correctly finds departments with more than 10 employees?
SELECT dept, AVG(salary) FROM employees GROUP BY dept HAVING AVG(salary) > 50000 ORDER BY AVG(salary) DESC; Which keyword can be replaced with a column alias?
Which query finds departments where ALL employees earn more than 50000?
Ready to Master SQL GROUP BY and HAVING?
Practice with adaptive questions. Track your misconceptions. Build real mastery.
Sign Up Free & Start PracticingFree account. No credit card required.
Ad Space