beginnerSELECT Queries — WHERE, GROUP BY, HAVING, ORDER BY

What is the difference between WHERE and HAVING?

WHERE filters individual rows BEFORE grouping — it reduces the input to GROUP BY. Cannot use aggregate functions. HAVING filters groups AFTER GROUP BY — applied to the result of aggregation. Can use aggregate functions. Performance: WHERE is more efficient because it reduces data before aggregation. Use WHERE whenever possible; only use HAVING for aggregate conditions. Example: to find departments

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Why should you avoid OFFSET for pagination in large tables?← Back to all SQL questions