The HAVING clause in SQL is used to filter data after it has been grouped with the GROUP BY clause. While the WHERE clause filters rows before the grouping process, HAVING filters groups that result from the GROUP BY operation. It is particularly useful when you need to apply conditions to aggregate functions like SUM(), COUNT(), AVG(), MAX(), and MIN().
This chapter will provide a comprehensive understanding of the HAVING
clause, explaining its uses from basic to advanced, along with examples, code explanations, and outputs. By the end, you’ll have a complete understanding of how to use HAVING
in SQL.
The HAVING
clause allows you to filter groups of data after the grouping has been performed with the GROUP BY
clause. It is used to set conditions on aggregate functions, which the WHERE
clause cannot handle.
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SUM()
, COUNT()
, AVG()
, MAX()
, etc.The primary difference between WHERE
and HAVING
is when the filtering takes place:
GROUP BY
clause.GROUP BY
clause.WHERE
cannot be used with aggregate functions like SUM()
, COUNT()
, AVG()
, etc., while HAVING
can.
HAVING
Consider a Sales
table:
SaleID | ProductID | Quantity | Price | SalesPerson | SaleDate |
---|---|---|---|---|---|
1 | 101 | 5 | 10 | John | 2023-01-10 |
2 | 102 | 2 | 20 | Sarah | 2023-01-11 |
3 | 101 | 4 | 10 | John | 2023-01-12 |
4 | 103 | 3 | 15 | John | 2023-01-12 |
5 | 102 | 6 | 20 | Sarah | 2023-01-13 |
Task: Find salespersons who sold more than 5 units in total.
SELECT SalesPerson, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY SalesPerson
HAVING SUM(Quantity) > 5;
SalesPerson | TotalQuantity |
---|---|
John | 12 |
Sarah | 8 |
SalesPerson
.SUM(Quantity)
calculates the total quantity sold for each salesperson.HAVING
clause filters the result set to only include those salespeople who have sold more than 5 units.You can apply multiple conditions in the HAVING
clause using logical operators such as AND
and OR
. For example, let’s extend the previous scenario and filter salespeople who sold more than 5 units and made a total sales amount greater than 100.
SELECT SalesPerson, SUM(Quantity) AS TotalQuantity, SUM(Quantity * Price) AS TotalSales
FROM Sales
GROUP BY SalesPerson
HAVING SUM(Quantity) > 5 AND SUM(Quantity * Price) > 100;
SalesPerson | TotalQuantity | TotalSales |
---|---|---|
Sarah | 8 | 160 |
SalesPerson
.SUM(Quantity * Price)
calculates the total sales for each salesperson.HAVING
clause ensures that only salespeople who sold more than 5 units and made total sales greater than 100 are returned.HAVING
with COUNT()
Suppose you have a Customers
table:
CustomerID | Name | City |
---|---|---|
1 | Alice | New York |
2 | Bob | Los Angeles |
3 | Charlie | New York |
4 | David | New York |
5 | Eve | Los Angeles |
Task: Find cities that have more than 2 customers.
SELECT City, COUNT(*) AS CustomerCount
FROM Customers
GROUP BY City
HAVING COUNT(*) > 2;
City | CustomerCount |
---|---|
New York | 3 |
City
.COUNT(*)
calculates the number of customers in each city.HAVING
clause filters to include only cities with more than 2 customers.Task: Find salespeople who have an average sale price greater than 15.
SELECT SalesPerson, AVG(Price) AS AveragePrice
FROM Sales
GROUP BY SalesPerson
HAVING AVG(Price) > 15;
SalesPerson | AveragePrice |
---|---|
Sarah | 20 |
SalesPerson
.AVG(Price)
calculates the average price of sales for each salesperson.HAVING
clause filters to include only salespeople with an average sale price greater than 15.You can use ORDER BY
along with HAVING
to sort the filtered results. For instance, you might want to list salespeople by total sales in descending order, but only include those who sold more than 10 units.
SELECT SalesPerson, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY SalesPerson
HAVING SUM(Quantity) > 10
ORDER BY TotalQuantity DESC;
SalesPerson | TotalQuantity |
---|---|
John | 12 |
SUM(Quantity) > 10
using HAVING
.ORDER BY
clause sorts the result by TotalQuantity
in descending order.Consider a scenario where you have a Sales
table with SaleDate
, and you want to find salespeople who made more than 5 sales in January 2023.
SELECT SalesPerson, COUNT(SaleID) AS SalesCount
FROM Sales
WHERE SaleDate BETWEEN '2023-01-01' AND '2023-01-31'
GROUP BY SalesPerson
HAVING COUNT(SaleID) > 5;
WHERE
clause filters rows for sales made in January 2023.SalesPerson
and counts the number of sales.HAVING
clause filters to include only salespeople with more than 5 sales.The HAVING clause is a powerful tool in SQL for filtering grouped data after aggregation. While the WHERE clause is useful for filtering individual rows before grouping, HAVING allows you to filter on aggregate results. Whether you're working with SUM(), COUNT(), AVG(), or other aggregate functions, HAVING can help you refine your results to meet specific conditions. Happy coding !❤️