The HAVING clause in SQL is used with the GROUP BY clause and returns the results that match the criteria set out in the HAVING clause.

Structure

SELECT EXPRESSION
FROM TABLE
WHERE [CONSTRAINTS]
GROUP BY [EXPRESSION]
HAVING [CONDITION]

Example

Suppose we want to find duplicate user names in our users table. Here is how we go about it:

Select username, COUNT(*) AS "Number of duplicates"
FROM users
GROUP BY username
HAVING COUNT(*) > 1;