Explain the difference between WHERE and HAVING clauses in SQL

SQL
Medium
149.4K views

This question tests the ability to filter data at different stages of query execution and aggregation.

Why Interviewers Ask This

Understanding the execution order of SQL clauses is crucial for writing efficient and correct queries. Candidates must distinguish between filtering raw rows and filtering aggregated groups.

How to Answer This Question

State that WHERE filters rows before grouping, while HAVING filters after aggregation. Explain that WHERE cannot use aggregate functions, whereas HAVING is designed for them. Provide a clear example contrasting the two.

Key Points to Cover

  • WHERE: pre-aggregation filtering
  • HAVING: post-aggregation filtering
  • Aggregate function usage
  • Execution order importance

Sample Answer

WHERE filters individual rows before any grouping or aggregation occurs, so it cannot use aggregate functions like SUM or COUNT. It is best for narrowing down raw data early, such as filtering by date range. In contrast,…

Common Mistakes to Avoid

  • Using aggregates in WHERE
  • Confusing the timing of execution
  • Not providing examples

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 69 SQL questions