WHERE vs HAVING in SQL comes down to timing. WHERE filters rows before they are grouped. HAVING filters groups after GROUP BY has made them. That is why conditions on totals and counts, such as SUM(amount) > 1000, belong in HAVING, and conditions on plain columns, such as city = 'Hyderabad', belong in WHERE.
WHERE vs HAVING: the difference table
| Point | WHERE | HAVING |
|---|---|---|
| Filters | Individual rows | Groups made by GROUP BY |
| Runs | Before grouping | After grouping |
Aggregate functions (SUM, COUNT) |
Not allowed | Allowed, and the usual reason to use it |
Needs GROUP BY |
No | Usually used with it |
| Works with | SELECT, UPDATE, DELETE |
SELECT only |
| Can use indexes to find rows | Yes | Not for the aggregate condition |
| Typical example | WHERE city = 'Hyderabad' |
HAVING COUNT(*) >= 2 |
In one line: WHERE picks the rows, HAVING picks the groups.
The sample table
All results on this page come from this table. They were run in SQLite; MySQL gives the same rows.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer VARCHAR(30),
city VARCHAR(30),
amount INTEGER
);
INSERT INTO orders VALUES
(1, 'Asha', 'Hyderabad', 1200),
(2, 'Ravi', 'Warangal', 300),
(3, 'Asha', 'Hyderabad', 800),
(4, 'Meena', 'Hyderabad', 450),
(5, 'Ravi', 'Warangal', 900),
(6, 'Kiran', 'Vijayawada', 200),
(7, 'Meena', 'Hyderabad', 700);
Without any filter, each customer's total is:
| customer | total |
|---|---|
| Asha | 2000 |
| Kiran | 200 |
| Meena | 1150 |
| Ravi | 1200 |
One query pair that shows the difference
The two queries below look similar, but they answer different questions.
Query 1, WHERE: total of the orders above 500 for each customer.
SELECT customer, SUM(amount) AS total
FROM orders
WHERE amount > 500
GROUP BY customer
ORDER BY customer;
customer total
Asha 2000
Meena 700
Ravi 900
Query 2, HAVING: customers whose total of all orders is above 1000.
SELECT customer, SUM(amount) AS total
FROM orders
GROUP BY customer
HAVING SUM(amount) > 1000
ORDER BY customer;
customer total
Asha 2000
Meena 1150
Ravi 1200
Compare Meena's row in both results:
- In Query 1, WHERE threw away her 450 order before grouping, so her total became 700.
- In Query 2, all her orders were added first (450 + 700 = 1150), and then HAVING checked the total.
Ravi changes the same way: 900 with WHERE (his 300 order was removed), 1200 with HAVING. Kiran disappears from both, but for different reasons: in Query 1 his only order (200) fails amount > 500, and in Query 2 his group total (200) fails SUM(amount) > 1000.
Same table, same columns, different answers. That is the difference between WHERE and HAVING.
Using WHERE and HAVING together
Most real queries use both. Question: in Hyderabad, which customers placed at least two orders?
SELECT customer, COUNT(*) AS orders_count, SUM(amount) AS total
FROM orders
WHERE city = 'Hyderabad'
GROUP BY customer
HAVING COUNT(*) >= 2
ORDER BY customer;
customer orders_count total
Asha 2 2000
Meena 2 1150
Here is the order in which the database thinks about this query:
- FROM reads the
orderstable. - WHERE keeps only Hyderabad rows (orders 1, 3, 4 and 7).
- GROUP BY makes one group per customer: Asha and Meena.
- HAVING keeps groups with at least two orders. Both pass.
- SELECT computes the output columns.
- ORDER BY sorts the result.
Remember this order and you will always know where a condition goes. A condition can only use what exists at that step: at step 2, there are rows but no groups, so no COUNT.
The common mistake: aggregates in WHERE
Beginners often write this:
SELECT customer, SUM(amount) AS total
FROM orders
WHERE SUM(amount) > 1000
GROUP BY customer;
It fails. The errors are:
SQLite: misuse of aggregate: SUM()
MySQL: ERROR 1111 (HY000): Invalid use of group function
At the WHERE step, the database is looking at one row at a time. SUM(amount) for a customer does not exist yet. Move the condition to HAVING, as in Query 2.
Common mistake: The opposite mistake does not give an error, so it is easier to miss.
HAVING customer <> 'Kiran'works and returns the right rows, but it groups Kiran's orders first and only then throws the group away.WHERE customer <> 'Kiran'removes his rows before grouping. Both return Asha 2000, Meena 1150 and Ravi 1200 here, but WHERE does less work and is the clearer choice. Keep plain column conditions in WHERE.
Combining both conditions on the same column
You can filter rows and groups on related ideas in one query. Customers whose orders above 500 add up to more than 1000:
SELECT customer, SUM(amount) AS total
FROM orders
WHERE amount > 500
GROUP BY customer
HAVING SUM(amount) > 1000
ORDER BY customer;
customer total
Asha 2000
WHERE removed small orders, leaving Asha 2000, Meena 700 and Ravi 900. HAVING then kept only totals above 1000. Only Asha passes.
Pro tip: Before writing HAVING, run the query without it. Look at the grouped totals, decide which groups you want, then add the HAVING line. This one habit catches most WHERE vs HAVING mix-ups.
Quick rules to remember
- Condition uses
SUM,COUNT,AVG,MINorMAX: put it in HAVING. - Condition uses a plain column value: put it in WHERE.
- Need to filter rows in
UPDATEorDELETE: only WHERE works there. - Writing for PostgreSQL, SQL Server or Oracle: repeat the expression in HAVING (
HAVING SUM(amount) > 1000), because an alias such astotalis not accepted there.
For freshers: how to answer in an interview
You can say this in about 30 seconds:
"WHERE filters rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and is used for conditions on SUM, COUNT and other aggregates. The logical order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. I use WHERE for row conditions because it reduces data before grouping."
Then show Query 1 and Query 2 on a whiteboard. Practise more in GROUP BY and HAVING interview questions, learn grouping step by step in GROUP BY in SQL, and see analytics-style examples in SQL GROUP BY and HAVING for analytics. CodeBegun teaches SQL in the data analytics course, in Madhapur, Hyderabad and live online.
Interview note: A follow-up you may get is "Can HAVING be used without GROUP BY?" Yes. The whole table is treated as one group.
SELECT COUNT(*) FROM orders HAVING COUNT(*) > 5returns7for this table, and returns no row at all if you change the condition to> 10.
Frequently Asked Questions
Can we use HAVING without GROUP BY?
Can we use WHERE and HAVING in the same query?
Why can't we use aggregate functions in WHERE?
Which is faster, WHERE or HAVING?
Can HAVING use a column alias from SELECT?
Want to Build Your Career in Data Analytics with AI?
Join CodeBegun and train with working industry engineers — Explore the Data Analytics program

