English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Use SQL HAVING Keyword and Examples

SQL Keyword Reference

HAVING

Use the HAVING command instead of WHERE and aggregate functions.

The following SQL lists each country/The number of customers in the region. But only query countries with5countries with more than one customer/Region:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL lists each country/The number of customers in the region, sorted from high to low (including only countries with5countries with more than one customer/Region):

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

SQL Keyword Reference