WHERE filters individual rows before they are grouped, and HAVING filters groups after aggregation. Use WHERE for conditions on ordinary columns and HAVING for conditions on aggregate results such as COUNT(*) > 5 or SUM(total) > 1000. The where vs having question is also a classic interview topic, and it comes down to the order in which MySQL evaluates a query.
A query can contain both clauses: WHERE trims the rows first, then GROUP BY builds groups, then HAVING removes the groups you do not want.
WHERE | HAVING | |
|---|---|---|
| Filters | Rows | Groups |
| Runs | Before GROUP BY | After GROUP BY and aggregation |
| Aggregate functions allowed | No | Yes |
| Can use select-list aliases | No | Yes (MySQL extension) |
Works without GROUP BY | Yes | Yes, but rarely useful |
| Can use indexes to find rows | Yes | No, it filters afterwards |
The logical order of a SELECT is: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT. Because WHERE runs before any group exists, it cannot know a group's COUNT or SUM. HAVING runs after, so it can.
The examples use an orders table:
Find customers whose paid orders add up to more than 100:
Step by step: WHERE status = 'paid' removes the two cancelled orders. GROUP BY customer makes four groups. HAVING SUM(total) > 100 drops Ravi (80.50) and Meera (30.00), leaving two groups.
Putting an aggregate in WHERE is an error:
Putting a plain row condition in HAVING often works, but it is wasteful. This query returns the right rows, yet MySQL builds groups for cancelled orders and only then throws them away:
It is also unreliable. status is neither grouped nor aggregated, so under ONLY_FULL_GROUP_BY the server rejects it, and without that mode it tests an arbitrary row of each group. Rule of thumb: if the condition mentions only columns of a single row, put it in WHERE.
Without GROUP BY, the whole result is one group, and HAVING tests an aggregate of the whole table:
The query returns one row if revenue is above 1000 and no row otherwise. It is valid, but unusual.
WHERE conditions can use indexes and reduce the rows MySQL has to read and group. HAVING conditions are applied to finished groups, so they cannot reduce the work of grouping. Always move every condition you can into WHERE, and leave only the aggregate conditions in HAVING.
A practical checklist:
COUNT, SUM, AVG, MIN or MAX? Use HAVING.WHERE.WHERE, aggregate part in HAVING.EXPLAIN to confirm an index serves the WHERE filter.For example, "customers with at least two orders in 2026":
Here the date range sits in WHERE, so an index on created_at can be used, and the order count sits in HAVING. (The orders table above would need a created_at column for this query.)
Yes. MySQL lets HAVING refer to select-list aliases, such as HAVING spent > 100. WHERE cannot, because the alias does not exist yet when WHERE runs.
Yes, and that is the normal pattern: WHERE filters rows, then GROUP BY groups them, then HAVING filters the groups.
The whole result is treated as a single group. It is rarely useful, and a WHERE or a subquery is usually clearer.
WHERE, when the same condition could go in either, because it reduces the rows before grouping and can use an index.
WHERE filters rows before grouping; HAVING filters groups after aggregation.HAVING and not in WHERE.WHERE so indexes help and fewer rows are grouped.WHERE, then GROUP BY, then HAVING.FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT explains the rules.