# SQL HAVING Clause

### The SQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

#### HAVING Syntax

```sql
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
```

### Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:<br>

![](https://826093633-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LS8lPNzCGPR5-DLbGXv%2F-LSc5BSZPUVebjbZRSN5%2F-LSc5cLqeovDTFZZc63U%2Fimage.png?alt=media\&token=ef24f62c-3e90-4494-bc75-86f43a446190)

\
SQL HAVING Examples

The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:

```sql
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
```

The following SQL statement lists the number of customers in each country, sorted high to low (Only include countries with more than 5 customer ):

```sql
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
```

**Demo Database**

Below is a selection from the "Orders" table in the Northwind sample database:\ <br>

![](https://826093633-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LS8lPNzCGPR5-DLbGXv%2F-LSc5BSZPUVebjbZRSN5%2F-LSc6E0KvI9VxraKZzYY%2Fimage.png?alt=media\&token=e04539e6-b65b-4ea8-b210-2b8856b05461)

\
And a selection from the "Employees" table:<br>

![](https://826093633-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LS8lPNzCGPR5-DLbGXv%2F-LSc5BSZPUVebjbZRSN5%2F-LSc6N2yNnunk9PkobZu%2Fimage.png?alt=media\&token=3a97cb58-a7a8-466a-bf77-c36d08951a37)

\
More HAVING Examples

The following SQL statement lists the employees that have registered more than 10 orders:

```sql
SELECT Employees.LastName, COUNT(Orders.OrderID)
 AS NumberOfOrders
FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID 
 Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
```

The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more than 25 orders:

```sql
SELECT Employees.LastName, COUNT(Orders.OrderID)
 AS NumberOfOrders
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID 
 Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller'
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;
```

\
NOTE: Whenever we want to apply filter condition in Aggregate functions, we use HAVING Clause. For Scalar Functions, we use WHERE Clause.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-sql.gitbook.io/sql-programming/sql-select/sql-having-clause.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
