WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL HAVING Clause


The HAVING Clause

The HAVING condition was added to SQL in light of the fact that the WHERE watchword couldn't be utilized with total functions.

SQL HAVING Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name administrator value
Gathering BY column_name
HAVING aggregate_function(column_name) administrator esteem;

Database

In this instructional exercise we will utilize the database.

Below is a determination from the "Orders" table:

OrderID CustomerID EmployeeID OrderDate ShipperID
10248 90 5 1994-02-11 3
10249 81 6 1999-01-06 1
10250 34 4 2018-04-08 2

And a determination from the "Employees" table:

EmployeeID LastName FirstName BirthDate Photo Notes
1 Maria R. Fern January 14, 1972 EmpID1.pic Education incorporates a BA....
2 Martin F. Thompson November 5, 1993 EmpID2.pic Andrew got his BTS....
3 Percy B. Campbell January 5, 1986 EmpID3.pic Janet has a BS degree....

SQL HAVING Example

Now we need to find  if any of the representatives has enrolled more than 10 orders.

We utilize the accompanying SQL statement:

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;

Now we need to discover if the workers "Davolio" or "Fuller" have enlisted more than 25 orders.

We add a normal WHERE provision 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;