Filtering Groups with HAVING (WHERE vs HAVING)
Filter rows before grouping with WHERE and groups after aggregation with HAVING.
In plain words
WHERE throws away rows you don't want; HAVING throws away groups you don't want.
Think of it like…
WHERE is the bouncer checking each person at the door; HAVING is the manager who looks at every table after people sit down and clears tables with fewer than four guests.
Where you’ve seen it
"Customers who spent over ₹10,000 on paid orders this year": WHERE status = 'paid' AND created_at >= '2026-01-01' filters orders, then GROUP BY customer_id HAVING SUM(amount) > 10000 filters customers.
Words you’ll hear
- WHERE
- Row-level filter applied before grouping.
- HAVING
- Group-level filter applied after GROUP BY.
- Aggregate
- A function over many rows: COUNT, SUM, AVG, MIN, MAX.
- HAVING without GROUP BY
- Treats the whole result as a single group.
Visual diagram
Theory & example queries
The rule of thumb
If the condition is about a single row (a status, a date, a category), it belongs in WHERE. If it's about a group (a count, a total, an average), it belongs in HAVING.
Putting a row condition in HAVING (HAVING status = 'paid') is an error if status isn't grouped. If it is grouped, the query works but filters after grouping, so the database aggregated rows it then throws away.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT customer_id, SUM(amount)FROM ordersWHERE status = 'paid'GROUP BY customer_idHAVING SUM(amount) > 10000;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative qualifying customers3 sample rows
| customer_id | sum |
|---|---|
| 17 | 12850 |
| 42 | 17340 |
| 91 | 10125 |
Note: Assumes these customers have paid-order totals above 10,000; values and currency units are illustrative.
Key takeaway: Row condition → WHERE. Aggregate condition → HAVING.
Edge cases interviewers like
HAVING can use aggregates that aren't in the SELECT list: HAVING COUNT(DISTINCT product_id) >= 3 is valid even if you only select customer_id.
HAVING without GROUP BY treats the whole table as one group, so it returns one row or none. Standard SQL doesn't allow SELECT aliases in HAVING: PostgreSQL and SQL Server reject them, MySQL accepts them. Repeating the aggregate is the portable choice.
SELECT user_idFROM purchasesGROUP BY user_idHAVING COUNT(DISTINCT product_id) >= 3;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative qualifying users3 sample rows
| user_id |
|---|
| 7 |
| 23 |
| 91 |
Note: Each listed sample user is assumed to have bought at least three distinct products.
Key takeaway: Repeat the aggregate in HAVING instead of relying on an alias.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
FROM collects rows
All rows from the table, plus any joins, form the starting set.
FROM ordersWhen to use it & trade-offs
What it is
WHERE filters individual rows before they are grouped. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT(*), SUM() or AVG(). Many real queries use both.
Why it matters: Mixing them up either throws an error (aggregates in WHERE) or quietly slows a query down (row filters in HAVING run after the expensive grouping). It's one of the most common SQL screening questions.
When to reach for it
- Finding groups above or below a threshold (customers with more than 5 orders)
- Detecting duplicates: GROUP BY email HAVING COUNT(*) > 1
- Filtering rows first with WHERE, then groups with HAVING
When to avoid or be careful
- Row-level conditions in HAVING
- SELECT aliases in HAVING if the query must run on PostgreSQL or SQL Server
- HAVING on a column that is neither grouped nor aggregated
WHERE vs HAVING vs a subquery
Strengths: Runs before grouping, can use indexes, reduces work.
Watch out: Can't reference aggregates.
Strengths: Filters on each group's computed result.
Watch out: Runs after grouping, so it's wasteful for row conditions.
Strengths: Lets you name the aggregate and filter or join on it.
Watch out: More verbose than HAVING for simple thresholds.
Interview signals & numbers to know
When this topic comes up
- “Find customers with more than N orders”
- “Find duplicate emails”
- WHERE vs HAVING
- “aggregate functions are not allowed in WHERE”
- GROUP BY with a threshold
Benchmarks & rules of thumb
- WHERE
- before GROUP BY
- HAVING
- after GROUP BY
- COUNT(*) > 1
- the classic duplicate check
Common mistakes & follow-ups
Common interview mistakes
- Writing
WHERE COUNT(*) > 1to find duplicates. - Moving a row filter into HAVING and making the query slower.
- Referencing a SELECT alias in HAVING and getting "column does not exist" in PostgreSQL.
Likely interviewer follow-ups
- Can you use HAVING without GROUP BY? What does it return?
- How would you find emails that appear more than once in a users table?
- Is there a performance difference between filtering a grouped column in WHERE versus HAVING?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Filtering Groups with HAVING (WHERE vs HAVING) with hands-on SQL practice and interview Q&A.