SQLite HAVING Clause
The HAVING Clause
WHERE filters rows before they are grouped, and it cannot use aggregate functions. HAVING filters the groups, after GROUP BY.
This example lists the countries with more than 5 customers:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
Try it Yourself »
HAVING With ORDER BY
Example
List the countries with more than 5 customers, sorted from the most customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
Try it Yourself »
HAVING With JOIN
This example lists the employees that have handled more than 25 orders:
Example
SELECT Employees.LastName,
COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;
Try it Yourself »