Articles

Difference Between WHERE and HAVING Clause in SQL

Discover the key differences between SQL's WHERE and HAVING clauses. Learn their syntax and use cases and how to apply them effectively in data filtering.

Introduction to WHERE and HAVING clauses

When working with SQL queries, filtering data is essential for retrieving only the most relevant information. One common comparison in SQL is WHERE vs HAVING, as both clauses refine query results, improve efficiency, and ensure accuracy. These clauses are beneficial in scenarios like analyzing sales data, managing customer records, or extracting insights from large datasets.

However, filtering does not always happen at the same stage of query execution. Some conditions must be applied before aggregation, while others make sense only after grouping data. How do you determine where to use your filters—before or after aggregation?

This is where a clear understanding of WHERE vs. HAVING in SQL is essential for writing efficient queries. Let’s begin by exploring the WHERE clause.

Related Course

Learn SQL

In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Try it for free

Using the WHERE clause in SQL

The WHERE clause filters rows before aggregation occurs in SQL. It helps narrow the data set based on specific conditions, making it an essential tool for raw data.

The syntax for the WHERE clause is as follows:

SELECT column1, column2, ... 
FROM table_name 
WHERE condition; 

For example, consider a table named customers with the following structure and data:

customer_id name city age
1 Amit Sharma New Delhi 28
2 Priya Mehta Mumbai 32
3 Raj Verma New Delhi 45
4 Neha Singh Bangalore 29
5 Vikas Rao New Delhi 35

Let’s say we need to filter and retrieve only customers located in New Delhi. The SQL query would look like this:

SELECT * FROM customers
WHERE city = 'New Delhi';

In this case, the WHERE clause filters out rows where the city is not ‘New Delhi’, ensuring that only relevant data is retrieved. The output will be:

customer_id name city age
1 Amit Sharma New Delhi 28
3 Raj Verma New Delhi 45
5 Vikas Rao New Delhi 35

Now, what if we need to filter data after grouping it? This is where the HAVING clauses come in. Let’s explore how it works.

Using the HAVING clause in SQL

While the WHERE clause filters data before aggregation, the HAVING clause filters the results after aggregation. It applies conditions to grouped data, allowing filtering based on aggregate functions like SUM(), COUNT(), or AVG().

The syntax for HAVING is as follows:

SELECT column1, column2, ..., AGGREGATE_FUNCTION(column) 
FROM table_name 
GROUP BY column1, column2, ... 
HAVING condition; 

Consider a products table with the following structure and sample data:

product_id product_name sales
1 Laptop 8000
2 Smartphone 12000
3 Tablet 5000
4 Monitor 15000
5 Headphones 7000

Let’s say we want to calculate the total sales per product and filter out those with sales greater than $10,000. The SQL query would look like this:

SELECT product_name, SUM(sales) AS total_sales
FROM products
GROUP BY product_name
HAVING SUM(sales) > 10000;

The output will be:

product_name total_sales
Smartphone 12000
Monitor 15000

Here, the HAVING clause ensures that the result includes only products with total sales exceeding $10,000.

Understanding the execution order of SQL queries is essential for grasping how the WHERE and HAVING clauses work fully. This order determines when each clause applies.

Understanding SQL query execution order

In SQL, queries execute in a specific order, which is crucial for effectively applying filters. This sequence determines how clauses interact and specifies where to apply each filter:

SQL Query Execution Order

The typical SQL query execution order is as follows:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

SQL Query Execution Funnel

The WHERE clause filters rows before grouping occurs, so it works on individual records. In contrast, the HAVING clause applies after grouping, allowing it to filter based on aggregate results. This distinction explains why you use aggregate functions like SUM() or COUNT() with HAVING but not WHERE.

To better visualize this process, a query execution flowchart can help illustrate the sequence:

SQL Filtering and Grouping Process

Now, let’s see an example where we might need to use both WHERE and HAVING and how the query is executed.

Consider a table Sales that tracks sales transactions with the columns -Product, Amount, and SaleDate:

Product Amount SaleDate
Apple 500 2024-01-05
Banana 200 2024-01-06
Apple 600 2024-02-01
Orange 300 2024-01-10
Banana 300 2024-02-02
Apple 700 2024-03-01

Suppose the task is to generate a report with the following criteria:

  1. Filter transactions that happened in 2024 only.
  2. Group the data by product to calculate the total sales amount for each product.
  3. Show only products where the total sales amount exceeds $1000.

The steps required to achieve this would be as follows:

  1. Filter by date: You need to consider only the transactions that occurred in 2024, which are done using the WHERE clause.
  2. Group by product: After filtering by date, you need to calculate the total sales for each product. This is done using GROUP BY.
  3. Filter by total sales: You want to include only products whose total sales exceed $1000. This is where the HAVING clause comes into play, filtering after the aggregation.

Hence, the SQL query would look like:

SELECT Product, SUM(Amount) AS TotalSales
FROM Sales
WHERE SaleDate >= '2024-01-01' -- Filters rows before grouping
GROUP BY Product
HAVING SUM(Amount) > 1000; -- Filters groups after aggregation

The output of the query will be as follows:

Product TotalSales
Apple 1800

This demonstrates how WHERE filters rows before grouping while HAVING filters the result of the aggregation.

Now that we’ve seen how both the WHERE and HAVING clauses help filter data, it’s important to understand how they differ in their application.

In the next section, let’s examine the key differences between WHERE and HAVING.

WHERE vs HAVING in SQL

Both WHERE and HAVING in SQL serve similar purposes, but they operate at different stages of query execution and apply in different contexts.

Here’s a table that highlights the key differences between the WHERE and HAVING clauses:

Feature WHERE HAVING
Purpose Filters rows before aggregation Filters groups after aggregation
Usage Applied to individual rows Applied to aggregated results (groups)
Can use aggregate functions? No Yes
SQL Position Before the GROUP BY clause After the GROUP BY clause
Example Use Case Filtering customers from a specific city Filtering products with total sales above a threshold

Conclusion

In conclusion, understanding the WHERE and HAVING clauses is essential for writing efficient SQL queries. While both filter data, the WHERE clause operates on individual rows before aggregation, whereas the HAVING clause filters aggregated results after grouping. Recognizing the SQL query execution order clarifies when and where to apply these clauses, ensuring precision in data retrieval.

Applying these concepts to real-world queries will enhance your ability to filter and aggregate data effectively, making your SQL queries powerful and efficient.

To dive deeper into SQL concepts and further enhance your skills, check out Codecademy’s Learn SQL course. Whether you’re a beginner or looking to refine your knowledge, this resource offers hands-on practice and valuable insights to boost your SQL expertise.

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team