← Back to Questions
SQL

What is the difference between WHERE and HAVING?

Learn What is the difference between WHERE and HAVING? with simple explanations, real-time examples, interview tips and practical use cases.

WHERE and HAVING are SQL clauses used to filter data, but they work at different stages of query execution.

In simple words:

  • WHERE filters rows before grouping
  • HAVING filters groups after grouping

Simple Understanding

Clause Filters
WHERE Individual Rows
HAVING Grouped Data

Why WHERE and HAVING are Important

Databases often contain:

  • Millions of records

Filtering helps:

  • Reduce unnecessary data
  • Improve query performance
  • Generate meaningful reports

Real-Time Example

Suppose an e-commerce platform stores order details.


Orders Table

Order ID Customer Amount
101 Naresh 5000
102 Rahul 3000
103 Naresh 7000
104 Arjun 2000

What is WHERE Clause?

WHERE clause filters rows before:

  • Grouping
  • Aggregation

WHERE Syntax

SELECT column_names
FROM table_name
WHERE condition;

Example

SELECT *
FROM orders
WHERE amount > 4000;

Result

Order ID Customer Amount
101 Naresh 5000
103 Naresh 7000

Important Point

WHERE works on:

Individual rows


WHERE Internal Flow

Table Data
     |
     v
WHERE Condition
     |
     v
Filtered Rows

What is HAVING Clause?

HAVING clause filters grouped data after:

  • GROUP BY
  • Aggregate functions

HAVING Syntax

SELECT column_names,
       aggregate_function()

FROM table_name

GROUP BY column_name

HAVING condition;

Example

SELECT customer,
       SUM(amount) AS total

FROM orders

GROUP BY customer

HAVING SUM(amount) > 6000;

Step-by-Step Understanding

Step 1 - GROUP BY

Customer Total Amount
Naresh 12000
Rahul 3000
Arjun 2000

Step 2 - HAVING Filter

Condition:

SUM(amount) > 6000

Final Result

Customer Total Amount
Naresh 12000

Important Point

HAVING works on:

Grouped and aggregated data


HAVING Internal Flow

Table Data
     |
     v
GROUP BY
     |
     v
Aggregate Calculation
     |
     v
HAVING Condition
     |
     v
Filtered Groups

Main Difference Between WHERE and HAVING

Feature WHERE HAVING
Works On Rows Groups
Execution Stage Before GROUP BY After GROUP BY
Aggregate Functions Not Allowed Allowed
Purpose Filter rows Filter grouped data

Why Aggregate Functions Cannot Be Used in WHERE

WHERE executes:

Before grouping

At that stage:

  • Aggregate values do not exist yet

Invalid Example

SELECT customer,
       SUM(amount)

FROM orders

WHERE SUM(amount) > 5000

GROUP BY customer;

Error Reason

SUM(amount) is not available during WHERE execution.


Correct Query

SELECT customer,
       SUM(amount)

FROM orders

GROUP BY customer

HAVING SUM(amount) > 5000;

Query Execution Order

FROM
   |
   v
WHERE
   |
   v
GROUP BY
   |
   v
HAVING
   |
   v
SELECT
   |
   v
ORDER BY

WHERE + HAVING Together

Both clauses can be used in same query.


Example

SELECT customer,
       SUM(amount) AS total

FROM orders

WHERE amount > 2000

GROUP BY customer

HAVING SUM(amount) > 6000;

Step-by-Step Execution

WHERE

Filters rows:

amount > 2000

GROUP BY

Groups remaining rows by customer.


HAVING

Filters grouped totals:

SUM(amount) > 6000

Real-Time Banking Example

Banking systems use:

  • WHERE for filtering transactions
  • HAVING for customer spending analysis

Example

SELECT customer_id,
       SUM(transaction_amount)

FROM transactions

WHERE transaction_date >= '2026-01-01'

GROUP BY customer_id

HAVING SUM(transaction_amount) > 100000;

Real-Time E-Commerce Example

E-commerce platforms use:

  • WHERE for filtering active orders
  • HAVING for identifying top customers

Example

SELECT customer_id,
       COUNT(order_id)

FROM orders

WHERE status = 'DELIVERED'

GROUP BY customer_id

HAVING COUNT(order_id) > 10;

Real-Time Learning Platform Example

Learning platforms use:

  • WHERE for filtering completed courses
  • HAVING for identifying active learners

Example

SELECT student_id,
       COUNT(course_id)

FROM enrollments

WHERE status = 'COMPLETED'

GROUP BY student_id

HAVING COUNT(course_id) >= 5;

Advantages of WHERE

  • Filters early
  • Improves performance
  • Reduces processing load

Advantages of HAVING

  • Filters aggregated data
  • Useful for reports and analytics
  • Works with aggregate functions

Common Interview Mistake

Many developers confuse:

  • WHERE with HAVING

Easy Memory Trick

Clause Think Like
WHERE Filter rows first
HAVING Filter grouped results later

Related Learning Topics


WHERE vs HAVING Performance

WHERE is generally:

  • Faster than HAVING

Reason:

  • Filtering happens before grouping
  • Less data processed during aggregation

Best Practices

  • Use WHERE for row filtering
  • Use HAVING for aggregate filtering
  • Filter as early as possible
  • Avoid unnecessary HAVING clauses

Professional Interview Answer

WHERE and HAVING are SQL clauses used for filtering data, but they operate at different stages of query execution. WHERE filters individual rows before grouping and does not support aggregate functions. HAVING filters grouped or aggregated data after GROUP BY and supports aggregate functions like SUM, COUNT, AVG, MAX, and MIN. WHERE is mainly used for row-level filtering, while HAVING is used for filtering grouped results in reporting and analytical queries.


Why Interviewers Like This Answer

  • Clearly explains execution order
  • Includes aggregate function concept
  • Shows query processing understanding
  • Provides real-world SQL examples
  • Explains performance considerations

Frequently Asked Questions

What is difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters grouped data after grouping.

Can aggregate functions be used in WHERE?

No, aggregate functions cannot be used in WHERE clause.

Can HAVING be used without GROUP BY?

Yes, but it is mostly used with GROUP BY.

Which is faster WHERE or HAVING?

WHERE is usually faster because filtering happens before grouping.

When should HAVING be used?

HAVING should be used when filtering aggregated results.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.