Description

The SQL keyword HAVING is used instead of the WHERE keyword to define conditions that include aggregate functions.

The below SQL lists the number of customers by country and returns only the countries having more than 5 customers.

SELECT country, COUNT(*)
FROM customers
GROUP BY country
HAVING COUNT(*) > 5;

The below SQL lists the number of customers by country, sorted by country in descending order, and returns only the countries having more than 5 customers.

SELECT country, COUNT(*)
FROM customers
GROUP BY country
HAVING COUNT(*) > 5
ORDER BY country DESC;

Related Links