Skip to main content

HAVING

HAVING is a clause in SQL that allows you to filter result sets in a GROUP BY clause. It is used to mention conditions on the groups being selected. In other words, HAVING is mainly used with the GROUP BY clause to filter the results that a GROUP BY returns.

It’s similar to a WHERE clause, but operates on the results of a grouping. The WHERE clause places conditions on the selected columns, whereas the HAVING clause places conditions on groups created by the GROUP BY clause.

Syntax

The basic syntax is as follows:

SELECT column_name, function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
HAVING function(column_name) condition value;

Example

Suppose we have a Sales table with the following data:

SaleIDProductQuantity
1A30
2B20
3A100
4B50
5C60
6A70

And we want to find products which have total quantity sold more than 100. We can use the HAVING clause as follows:

SELECT Product, SUM(Quantity) as TotalQuantity
FROM Sales
GROUP BY Product
HAVING TotalQuantity > 100;

In this query,

  • GROUP BY Product would group the sales figures by Product.
  • SUM(Quantity) would calculate total quantity sold for each product.
  • HAVING TotalQuantity > 100 would filter out the groups which have total quantity sold less than or equal to 100.-