0Pricing
SQL Interview Prep · Lesson

HAVING vs WHERE

Filtering before grouping versus after, and which clause sees the aggregate.

The Question You Will Get

'What is the difference between WHERE and HAVING?' is one of the most asked SQL interview questions. A weak answer says 'HAVING is for aggregates.' A strong answer explains when each clause runs in the query pipeline.

That timing is the whole story: WHERE filters rows before grouping; HAVING filters groups after aggregation.

Where They Sit in Execution Order

Recall the logical execution order of a query:

  • FROM / JOIN → build the row set
  • WHERE → filter individual rows
  • GROUP BY → collapse into groups
  • HAVING → filter the groups
  • SELECT → project columns
  • ORDER BY → sort

WHERE happens before groups exist; HAVING happens after, so HAVING can see aggregates and WHERE cannot.

All lessons in this course

  1. The GROUP BY Rule for SELECT Columns
  2. HAVING vs WHERE
  3. Grouping by Multiple Columns and Expressions
  4. Counting and Filtering Groups
← Back to SQL Interview Prep