Differences between WHERE and HAVING in SQL

Dimeji
3 min readMar 21, 2023

--

SQL is a powerful language used to manage and manipulate data stored in relational databases. When writing SQL queries, the WHERE and HAVING clauses are commonly used to filter data. While both clauses may seem similar at first glance, they are used in different contexts and have different functionalities. In this article, we will explore the differences between WHERE and HAVING clauses in SQL.

The functionality of WHERE and HAVING Clauses

The WHERE clause is used to filter rows from a table based on a specified condition. It is used in the SELECT, UPDATE, DELETE, and INSERT INTO statements. The WHERE clause filters rows before they are grouped and aggregated.

The HAVING clause is used to filter groups of rows based on a specified condition. It is used in the SELECT statement along with the GROUP BY clause. The HAVING clause filters rows after they are grouped and aggregated.

Syntax Differences between WHERE and HAVING Clauses

The syntax of the WHERE and HAVING clauses are different.

The WHERE clause appears in the SELECT statement before the GROUP BY clause. It filters rows based on a condition.

SELECT column1, column2, ...
FROM table
WHERE condition
GROUP BY column1, column2, ...;

The HAVING clause appears after the GROUP BY clause in the SELECT statement. It filters groups of rows based on a condition.

SELECT column1, column2, ...
FROM table
GROUP BY column1, column2, ...
HAVING condition;

Examples of WHERE and HAVING Clauses

To better understand the differences between WHERE and HAVING clauses, let’s consider an example using a table called “sales” with the columns “product”, “region”, and “sales_amount”.

Using WHERE:

Suppose we want to retrieve sales data for products in the “North” region only. We can use the following SQL query:

SELECT product, SUM(sales_amount) AS total_sales
FROM sales
WHERE region = 'North'
GROUP BY product;

This query uses the WHERE clause to filter rows based on the condition that the region is “North”. It then groups the rows by product and calculates the total sales for each product.

Using HAVING:

Suppose we want to retrieve sales data for products with a total sales amount greater than $1000. We can use the following SQL query:

SELECT product, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY product
HAVING SUM(sales_amount) > 1000;

This query groups the rows by product and calculates the total sales for each product. It then uses the HAVING clause to filter the groups based on the condition that the total sales amount is greater than $1000.

Conclusion

The WHERE and HAVING clauses in SQL are used to filter data in different contexts. The WHERE clause filters rows before they are grouped and aggregated, while the HAVING clause filters groups of rows after they are grouped and aggregated. By understanding the differences between these two clauses, we can write more effective SQL queries that retrieve the data we need.

--

--

Dimeji
Dimeji

Written by Dimeji

Dimeji is a tech enthusiast. He believes in developing and using innovative skills to improve processes, effect changes and affect people positively.

No responses yet