Understanding SQL
Filtering: The Differences
Between WHERE and
HAVING
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.
The WHERE clause is used to filter data before any grouping or
aggregation occurs, focusing on individual rows. On the other hand,
the HAVING clause is applied after data is grouped, allowing you to
filter aggregated results like totals, averages, or counts. Together, they
give SQL its flexibility and power in handling diverse filtering
requirements.
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:
1. FROM: Specifies the source tables.
2. WHERE: Filters individual rows.
3. GROUP BY: Groups data.
4. HAVING: Filters grouped data.
5. SELECT: Selects columns or expressions.
6. ORDER BY: Orders the result set.
The Role of WHERE
The WHERE clause filters rows before grouping or aggregation occurs.
It is often used to initialize the dataset's filtering.
Example 1: Filter Employees by Salary
Retrieve all employees with a salary greater than 50,000.
Pooja Pawar
Output:
employee_id employee_name salary
101 David Smith 60000
102 Jane Doe 70000
Here, rows with salary <= 50000 are excluded before aggregation or
grouping.
The Role of HAVING
The HAVING clause filters aggregated results after grouping.
Example 2: Filter Departments by Average Salary
Retrieve departments with an average salary greater than 50,000.
Pooja Pawar
Output:
department_id avg_salary
1 55000
2 60000
Here:
Data is grouped by department_id.
The HAVING clause filters out groups with an AVG(salary) ≤
50000.
Combining WHERE and HAVING
Using both WHERE and HAVING in the same query helps filter data
both before and after aggregation.
Pooja Pawar
Example 3: Combined Filtering
Retrieve departments where the average salary of employees
earning above 40,000 is greater than 50,000.
Output:
department_id avg_salary
1 55000
2 60000
Explanation:
1. WHERE salary > 40000: Filters individual rows.
2. GROUP BY department_id: Groups remaining rows by
department.
3. HAVING AVG(salary) > 50000: Filters grouped results.
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
Sales Data Analysis
Consider a sales table:
sale_id region product revenue quantity
1 East A 500 10
2 West B 300 5
3 East C 200 8
4 North A 700 12
5 West C 400 6
Scenario 1: Using WHERE
Retrieve all sales records where revenue exceeds 300.
Pooja Pawar
Output:
sale_id region product revenue
1 East A 500
4 North A 700
5 West C 400
Scenario 2: Using HAVING
Retrieve regions with total revenue greater than 800.
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
Common Errors and Misconceptions
1. Using Aggregate Functions in WHERE:
Reason: Aggregates are not allowed in WHERE. Use HAVING instead.
Pooja Pawar
2. Using HAVING Without GROUP BY:
Explanation: HAVING can work without GROUP BY if there's an
aggregate function.
Tips for Writing Efficient Queries
1. Use WHERE to Reduce Data Early: Apply WHERE to filter rows
before grouping, minimizing the workload on the database
engine.
2. Reserve HAVING for Aggregated Data: Avoid using HAVING for
conditions that can be handled by WHERE.
3. Combine WHERE and HAVING: Use both to optimize query
performance when filtering on raw data and aggregated results.
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