????????? ??? ??????s - ????? ?? ?????? – ????’? ??? ???? ??????????
????????? ??? ??????s - ????? ?? ?????? – ????’? ??? ???? ??????????
Pooja Pawar
Pooja Pawar
Introduction
When working with SQL, filtering data is one of the most fundamental
and essential tasks. SQL provides two powerful clauses—WHERE and
HAVING—to achieve this, but they are often misunderstood or
incorrectly used due to their subtle yet significant differences.
This guide dives deep into the distinctions between these clauses,
using practical examples, real-world scenarios, and clear explanations
to demystify their usage. Whether you're a beginner or an advanced
SQL user, understanding the interplay between WHERE and HAVING
will help you write more efficient and accurate queries.
By the end of this document, you’ll not only master the technical
differences but also gain the confidence to apply both clauses in
tandem for optimal results in real-world database operations. Let’s
explore the magic of WHERE and HAVING!
Pooja Pawar
Key Differences: HAVING vs WHERE
Aspect WHERE HAVING
Purpose Filters rows based on a Filters groups of data
condition before based on aggregated
grouping or aggregation. conditions after
grouping.
Works With Operates on raw, Operates on grouped or
unaggregated data aggregated data.
(individual rows).
Aggregation Does not work with Specifically designed to
aggregate functions like work with aggregate
SUM, AVG, MAX, etc. functions.
Execution Applied before the Applied after the
Order GROUP BY clause in the GROUP BY clause in the
query execution order. query execution order.
Condition Used to filter rows based Used to filter
Scope on column values or aggregated results of
expressions. groups or rows.
Syntax Can be used without Requires GROUP BY or
Requirement GROUP BY. an aggregate function.
Error Using aggregate Using aggregate
functions in WHERE functions in HAVING is
causes an error. valid and expected.
Pooja Pawar
SQL Query Execution Order
Understanding the SQL query execution order is critical for grasping
how WHERE and HAVING differ:
Pooja Pawar
Output:
Here, rows with salary <= 50000 are excluded before aggregation or
grouping.
Pooja Pawar
Output:
department_id avg_salary
1 55000
2 60000
Here:
Pooja Pawar
Example 3: Combined Filtering
Output:
department_id avg_salary
1 55000
2 60000
Explanation:
Pooja Pawar
Use Cases of WHERE vs HAVING
Use Case WHERE HAVING
Filtering data based WHERE salary > Not applicable.
on specific column 40000
values.
Filtering grouped or Not applicable. HAVING
aggregated data. COUNT(employee_id) > 5
Improving query WHERE filters HAVING does not impact
performance by rows before rows processed during
reducing rows aggregation. aggregation.
processed during
aggregation.
Working with Cannot use Required for filtering
aggregate functions aggregate aggregated data.
(e.g., SUM, AVG, functions.
COUNT).
Filtering data when Always Not applicable.
no aggregation is applicable.
involved.
Pooja Pawar
Real-World Scenarios
Pooja Pawar
Output:
Output:
region total_revenue
East 1200
West 900
Pooja Pawar
Scenario 3: Combining WHERE and HAVING
Retrieve regions with total revenue greater than 800, but only
include sales where the revenue per sale is greater than 300.
Output:
region total_revenue
West 900
Pooja Pawar
2. Using HAVING Without GROUP BY:
Pooja Pawar
Summary Table
Feature WHERE HAVING
Filters Rows Before grouping or After grouping or
aggregation. aggregation.
Supports No Yes
Aggregates
Execution Order Before GROUP BY. After GROUP BY.
Example WHERE salary > 50000 HAVING AVG(salary) >
50000
Pooja Pawar