Stop 05 · Aggregation & GroupingTopics · 4/9

WHERE vs HAVING

Part of the SQL Roadmap

In short

WHERE and HAVING both filter, so they feel interchangeable. They are not: one works on single rows before grouping, the other on finished groups, and mixing them up gives errors or slow queries.

WHERE filters individual rows before grouping, and HAVING filters groups after aggregation. Put row-level conditions in WHERE so less data reaches the GROUP BY.

The jaadu checks hall tickets of students at a classroom door in the morning, while in the background it hands a shiny trophy to one cheering group of students.

Think of it like this

The exam hall and the prize day

On exam day, the invigilator checks each student's hall ticket at the door. No ticket, no entry (WHERE, one person at a time). Weeks later, on prize day, the principal looks at each class's average marks and only classes above 75 get a trophy (HAVING, one group at a time). The principal can't judge a class average at the door, because the exam hasn't happened yet.

Watch it happen

Two filters, two moments

WHERE

  • Runs before GROUP BY
  • Looks at one row at a time
  • Can't use SUM, COUNT or AVG
  • Fewer rows reach the grouping, so it's faster

HAVING

  • Runs after GROUP BY
  • Looks at one finished group at a time
  • Made for conditions like SUM(amount) > 20000
  • Can only see grouped columns and aggregates

Row conditions go in WHERE. Conditions on totals, counts or averages go in HAVING. Most real queries use both.

The big ideas

What you need to know

Timing is everything

WHERE runs before rows are grouped, HAVING after. That single fact explains every rule below.

No aggregates in WHERE

WHERE SUM(amount) > 20000 is an error. The sum doesn't exist yet when WHERE runs.

Filter rows early

A condition like city = 'Kochi' works in HAVING too, but WHERE throws rows away before the costly grouping step.

Use both together

WHERE picks which rows count, HAVING picks which groups are shown. They answer different questions.

How it works

What happens to each order

  1. 1

    Read the rows

    Every order in the table is a candidate.

  2. 2

    WHERE

    Cancelled orders and orders outside October are dropped, one row at a time.

  3. 3

    GROUP BY

    The surviving rows are bundled into one group per customer.

  4. 4

    Aggregate

    SUM(amount) and COUNT(*) are calculated inside each group.

  5. 5

    HAVING

    Groups whose total is ₹20,000 or less are dropped.

See it in code

Walk through the code

sql
1SELECT2  customer_id,3  COUNT(*)    AS orders,4  SUM(amount) AS total_spent5FROM orders6WHERE status = 'delivered'7  AND order_date >= DATE '2026-10-01'8  AND order_date <  DATE '2026-11-01'9GROUP BY customer_id10HAVING SUM(amount) > 2000011ORDER BY total_spent DESC;

1/4Row filters: only delivered October orders enter the groups.

Customers who spent more than ₹20,000 on delivered orders during the Diwali month. MySQL also lets you write HAVING total_spent > 20000, but PostgreSQL and SQL Server need the full SUM(amount).

Save yourself the pain

Common mistakes

Oops

WHERE SUM(amount) > 20000

Do this instead

HAVING SUM(amount) > 20000. PostgreSQL will tell you aggregate functions are not allowed in WHERE.

Oops

Putting HAVING status = 'delivered' to filter rows.

Do this instead

Move it to WHERE. It gives the same answer here but groups far more rows than needed, and status must then be in GROUP BY.

Oops

HAVING total_spent > 20000 in PostgreSQL.

Do this instead

Repeat the expression: HAVING SUM(amount) > 20000. Only MySQL accepts SELECT aliases in HAVING.

Your mission

Split the filters

~20 min

Quick check

You want cities where at least 100 orders were delivered in 2026. Which is correct?

Go deeper

  1. 1Write one query that uses both: WHERE for a date range and HAVING for a minimum total.
  2. 2Try putting an aggregate like SUM(amount) > 1000 in WHERE and read the error.
  3. 3Move a non-aggregate condition from HAVING to WHERE and confirm the result is the same.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge