Backend & Databases Comparisonbeginner

WHERE vs HAVING in SQL: Differences

WHERE filters rows before they are grouped. HAVING filters the groups after GROUP BY. See one query pair with real results, the error beginners hit, and how to use both.

5 min read

Our verdict in 30 seconds

WHERE filters individual rows before they are grouped, so it cannot use aggregate functions such as SUM or COUNT. HAVING filters groups after GROUP BY, so it is where conditions on SUM, COUNT, AVG, MIN and MAX go. A query can use both: WHERE to pick the rows, GROUP BY to form groups, then HAVING to keep only the groups you want.

On This Page

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:

  1. FROM reads the orders table.
  2. WHERE keeps only Hyderabad rows (orders 1, 3, 4 and 7).
  3. GROUP BY makes one group per customer: Asha and Meena.
  4. HAVING keeps groups with at least two orders. Both pass.
  5. SELECT computes the output columns.
  6. 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, MIN or MAX: put it in HAVING.
  • Condition uses a plain column value: put it in WHERE.
  • Need to filter rows in UPDATE or DELETE: only WHERE works there.
  • Writing for PostgreSQL, SQL Server or Oracle: repeat the expression in HAVING (HAVING SUM(amount) > 1000), because an alias such as total is 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(*) > 5 returns 7 for 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?
Yes. Without GROUP BY, the whole result is treated as one group, so HAVING keeps or removes that single summary row. For example, SELECT COUNT(*) FROM orders HAVING COUNT(*) > 5 returns one row only if there are more than five orders. It is rare in real code.
Can we use WHERE and HAVING in the same query?
Yes, and it is common. WHERE runs first on rows, then GROUP BY forms groups, then HAVING filters those groups. Put row conditions in WHERE and aggregate conditions in HAVING.
Why can't we use aggregate functions in WHERE?
Because WHERE is checked one row at a time, before any groups exist. A value such as SUM(amount) is only known after the rows are grouped. That is why the database gives an error such as 'misuse of aggregate' in SQLite or 'Invalid use of group function' in MySQL.
Which is faster, WHERE or HAVING?
For conditions on normal columns, WHERE is usually better because it removes rows before grouping, so less data is grouped, and it can use indexes. Many optimisers move simple HAVING conditions into WHERE for you, but writing them in WHERE is clearer and safer.
Can HAVING use a column alias from SELECT?
MySQL and SQLite allow HAVING total > 1000 when total is an alias in SELECT. PostgreSQL, SQL Server and Oracle do not, so write HAVING SUM(amount) > 1000 there. Repeating the expression works everywhere.

Want to Build Your Career in Data Analytics with AI?

Join CodeBegun and train with working industry engineers — Explore the Data Analytics program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn

More comparisons

View all
Chat with us