In this article, we will learn how to use the HAVING clause in SQL.

The WHERE clause could not be used with Aggregate Functions, so the HAVING clause was added to SQL.

Syntax

SELECT column_name(s)
FROM TABLE_NAME
GROUP BY column_name(s)
HAVING condition;

Example

The subsequent statement would list the number of viewers in each city. Only include cities with more than 5 viewers:

SELECT COUNT(ID), City
FROM Viewers
GROUP BY City
HAVING COUNT(ID) > 5;

 

Also, check How To Use GROUP BY Keyword In SQL