Introduction 

When it comes to filtering data in SQL, developers often encounter the HAVING and WHERE statements. Though they might seem similar at first, they serve different purposes and are used in distinct contexts. In this blog, we will explore the differences between the HAVING and WHERE statements in SQL, along with two examples of each, to help you grasp their unique functionalities.

  1. WHERE Statement: The WHERE statement is used in SQL to filter rows based on specified conditions. It is primarily employed with the SELECT, UPDATE, and DELETE statements. The WHERE clause acts as a condition that filters rows before the data is grouped or aggregated.

Example 1: Selecting Customers from a Specific City Suppose we have a table called “Customers” with columns “CustomerID,” “CustomerName,” and “City.” We want to retrieve the names of customers who are from a particular city, say “New York.”

SELECT CustomerName FROM Customers WHERE City = ‘New York’;

This query will return all the customer names from the “Customers” table who reside in the city of “New York.”

Example 2: Updating Records Based on Specific Conditions Let’s consider a table called “Products” with columns “ProductID,” “ProductName,” “Quantity,” and “Price.” We want to update the “Quantity” of a specific product, say “Widget,” which is currently less than 50.

UPDATE Products SET Quantity = 100 WHERE ProductName = ‘Widget’ AND Quantity < 50;

This query will update the “Quantity” to 100 for the product “Widget” if its current quantity is less than 50.

  1. HAVING Statement: The HAVING statement is used with the GROUP BY clause and is employed to filter the results of aggregate functions based on specified conditions. It is used when we want to filter data after grouping and applying aggregate functions.

Example 3: Finding Average Order Amount per Customer Suppose we have a table called “Orders” with columns “OrderID,” “CustomerID,” and “Amount.” We want to find the average order amount for each customer and retrieve only those customers with an average order amount greater than $500.

SELECT CustomerID, AVG(Amount) AS AvgOrderAmount FROM Orders GROUP BY CustomerID HAVING AVG(Amount) > 500;

This query will calculate the average order amount for each customer and return only those customers with an average order amount greater than $500.

Example 4: Filtering Groups with Specific Conditions Let’s consider a table called “Employees” with columns “EmployeeID,” “Department,” and “Salary.” We want to find the departments where the average salary is more than $70,000.

SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department HAVING AVG(Salary) > 70000;

This query will calculate the average salary for each department and return only those departments where the average salary is more than $70,000.

Conclusion

In summary, the WHERE statement is used to filter rows before grouping or aggregating data, while the HAVING statement is used to filter the results of aggregate functions after grouping. Understanding the distinction between these two SQL statements is crucial for effectively manipulating data and obtaining the desired results from your queries. By mastering the usage of WHERE and HAVING statements, you can significantly enhance your SQL querying skills and efficiently manage your relational databases.

 

The Data School
Author: The Data School