SQL HAVING Example

SQL HAVING Example


|OrderID |EmployeeID|ShipperID  |
+--------+----------+-----------+
|10248	 | 5        |	3       |
+--------+----------+-----------+
|10249	 | 6        |	1       |
+--------+----------+-----------+
|10250	 | 4        |	2       |
And a selection from the "Employees" table:

|EmployeeID |LastName |
+-----------+---------+
|1          |Davolio  |
+-----------+---------+
|2          |Fuller   |
+-----------+---------+
|3          |Leverling|
Now we want to find if any of the employees has registered more than 10 orders.
We use the following SQL statement:
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;
Read more: SQL HAVING Clause

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
SQL snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes