You use the HAVING clause to specify search conditions for selecting groups. If a group satisfies the specified search condition, the row for that group is included in the derived table. If no GROUP BY clause is specified, all the rows are considered one group.
SELECT ...
HAVING
search_condition
search_condition
Search condition to be satisfied by a group.
Unlike a WHERE search condition, which is evaluated for each row in a table, the HAVING search condition is evaluated once for each group.
A column name in search_condition must satisfy one of the following conditions:
The column is included in the GROUP BY clause.
The column name is an argument of an aggregate function (AVG(), SUM(), ...). If the column name also appears in the SELECT list, it may also only appear there as the as an argument of an aggregate function.
The column occurs in a subquery. If the column name references the table in the FROM clause, it must be included in the GROUP BY clause or be the argument in an aggregate function.
The column is part of a table from a higher-level SELECT expression.
Example
Display the latest service provided for each order, but only if it was provided after the specified date:
SELECT order_num, MAX(service_date) FROM service GROUP BY order_num
HAVING MAX(service_date) > DATE'<date>'