SQL HAVING Clause

## SQL Basic

SQL HOME SQL Intro SQL Syntax SQL SELECT SQL SELECT DISTINCT SQL WHERE SQL AND & OR SQL ORDER BY SQL INSERT INTO SQL UPDATE SQL DELETE SQL Injection

## SQL Advanced

SQL SELECT TOP SQL LIKE SQL Wildcards SQL IN SQL BETWEEN SQL Aliases SQL Joins SQL INNER JOIN SQL LEFT JOIN SQL RIGHT JOIN SQL FULL JOIN SQL UNION SQL SELECT INTO SQL INSERT INTO SELECT SQL CREATE DB SQL CREATE TABLE SQL Constraints SQL NOT NULL SQL UNIQUE SQL PRIMARY KEY SQL FOREIGN KEY SQL CHECK SQL DEFAULT SQL CREATE INDEX SQL DROP SQL ALTER SQL Auto Increment SQL Views SQL Dates SQL NULL Values SQL NULL Functions SQL General Data Types SQL DB Data Types

## SQL Functions

SQL Functions SQL AVG() SQL COUNT() SQL FIRST() SQL LAST() SQL MAX() SQL MIN() SQL SUM() SQL GROUP BY SQL HAVING SQL UCASE() SQL LCASE() SQL MID() SQL LEN() SQL ROUND() SQL NOW() SQL FORMAT()
SQL Quick Ref SQL Hosting SQL Summary

## SQL Quiz

SQL Quiz

# SQL HAVING Clause

« Previous
Next Chapter »

## The HAVING Clause

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

### SQL HAVING Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;

## Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

OrderID | CustomerID | EmployeeID | OrderDate | ShipperID
--- | --- | --- | --- | ---
10248 | 90 | 5 | 1996-07-04 | 3
10249 | 81 | 6 | 1996-07-05 | 1
10250 | 34 | 4 | 1996-07-08 | 2

And a selection from the "Employees" table:

EmployeeID | LastName | FirstName | BirthDate | Photo | Notes
--- | --- | --- | --- | --- | ---
1 | Davolio | Nancy | 1968-12-08 | EmpID1.pic | Education includes a BA....
2 | Fuller | Andrew | 1952-02-19 | EmpID2.pic | Andrew received his BTS....
3 | Leverling | Janet | 1963-08-30 | EmpID3.pic | Janet has a BS degree....

## SQL HAVING Example

Now we want to find if any of the customers have a total order of less than 2000.

We use the following SQL statement:

The following SQL statement finds if any of the employees has registered more than 10 orders:

## Example

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;
Try it yourself »

Now we want to find the if the employees "Davolio" or "Fuller" have more than 25 orders

We add an ordinary WHERE clause to the SQL statement:

## Example

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;
Try it yourself »
« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE