WHERE vs GROUP BY vs HAVING in SQL
WHERE filters rows, GROUP BY builds groups and HAVING filters groups. See one worked example, the order SQL runs them in, and the mistakes to avoid.
Short answer: in SQL, WHERE filters individual rows before grouping, GROUP BY collects rows into groups, and HAVING filters those groups after they are formed. That is why WHERE cannot use SUM() or COUNT(), while HAVING can. If you remember the order WHERE, GROUP BY, HAVING, most mistakes disappear.
What Does Each Clause Do?
Clause | Works on | Can use aggregates like SUM()? |
|---|---|---|
WHERE | Single rows, before grouping | No |
GROUP BY | Turns rows into groups | Not applicable |
HAVING | Whole groups, after grouping | Yes |
A Worked Example
Take an orders table with these rows:
id | customer | status | amount |
|---|---|---|---|
1 | Asha | paid | 40 |
2 | Asha | paid | 60 |
3 | Ravi | refunded | 30 |
4 | Ravi | paid | 20 |
5 | Mei | paid | 50 |
6 | Mei | paid | 25 |
7 | Mei | paid | 45 |
We want customers whose paid orders add up to at least 100.
SELECT customer,
COUNT(*) AS orders,
SUM(amount) AS total
FROM orders
WHERE status = 'paid' -- 1. filter rows
GROUP BY customer -- 2. make one group per customer
HAVING SUM(amount) >= 100 -- 3. filter groups
ORDER BY total DESC;Running it returns two rows: Mei with 3 orders and a total of 120, then Asha with 2 orders and a total of 100. Ravi is gone for two reasons: his refunded order was removed by WHERE, and his remaining total of 20 fails HAVING.
In What Order Does SQL Run These Clauses?
You write SELECT first, but the database processes the query in a different logical order:
FROM: pick the table.WHERE: drop rows that do not match.GROUP BY: build the groups.HAVING: drop groups that do not match.SELECT: compute the columns.ORDER BY: sort the result.
This order explains the rule: when WHERE runs, the sums do not exist yet.
Can You Use WHERE and HAVING Together?
Yes, and you usually should. Put every condition that depends only on a single row in WHERE, because it shrinks the data before grouping. Use HAVING only for conditions on aggregates. The example above does exactly this.
Common Mistakes
Aggregate in WHERE:
WHERE SUM(amount) > 100fails with an error such as “misuse of aggregate”. Move it toHAVING.Row filter in HAVING:
HAVING status = 'paid'is not the right place. Many databases reject it, and it is slower where allowed. UseWHERE.Selecting a column that is not grouped: every selected column must be in
GROUP BYor inside an aggregate. Some databases are lenient, but results can then be unpredictable.Forgetting that COUNT(*) counts rows after WHERE: filtered rows are not counted.
Interview Tip
Interviewers love this question. A one-line answer: “WHERE filters rows before aggregation, HAVING filters groups after it.” Then add the example of total per customer above the limit. New to SQL? Start with What Is SQL and What Is It Used For?.
Key Takeaway
WHERE filters rows, GROUP BY makes groups, HAVING filters groups. Filter early with WHERE, summarise with GROUP BY, and use HAVING only when the condition needs an aggregate.
Ready to start? Practise SELECT, filtering, grouping and joins with short exercises you can run yourself. Begin with the free course SQL and Data Handling for AI: Beginner.
Related articles
More in SQL and Data
SQL vs Python for Data Analysis
SQL and Python do different jobs in data analysis. See what each is best at, a side-by-side example, and which one to learn first.
4 min readNov 15, 2026Primary Key vs Foreign Key in SQL
What primary and foreign keys are, how they differ, and how a runnable customers-and-orders example shows the database enforcing them.
4 min readNov 13, 202625 SQL Interview Questions for Freshers
The 25 SQL questions freshers meet most often, with short answers on keys, joins, NULL, indexes and classic query puzzles.
5 min readNov 7, 2026MySQL vs PostgreSQL vs SQLite: Which Should You Learn?
SQL is the language; MySQL, PostgreSQL and SQLite are database systems. See how they differ and which one to learn first.
3 min readNov 5, 2026SQL vs NoSQL: Differences and When to Use Each
SQL stores data in structured tables, NoSQL in flexible documents, key-values or graphs. Here are the differences, a comparison table and a simple guide to choosing.
4 min readNov 1, 2026What Is SQL and What Is It Used For?
SQL is the standard language for working with relational databases. Learn what it is, the four core statements, and why AI engineers need it.
3 min readOct 30, 2026
Comments
Sign in to join the discussion.

