The HAVING clause is employed in lieu of WHERE when filtering results based on aggregate functions. The subsequent SQL enumerates the count of customers in each country, including only countries with more than 5 customers:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5; |
The following SQL displays the count of customers in each country, sorted from highest to lowest, while only including countries with more than 5 customers:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5 ORDER BY COUNT(CustomerID) DESC; |