HAVING clause

The HAVING clause enables you to specify conditions for groups of records (for example, display only the departments that have salaries totaling more than $200,000). It has the following format:

Copy
HAVING expr1 rel_operator expr2

expr1 and expr2 can be field names, constant values, or expressions. These expressions do not have to match a column expression in the SELECT clause.

rel_operator is the relational operator that links the two expressions.

Example

Return only the departments whose sums of salaries are greater than $200,000.

Copy
SELECT dept_id, SUM (salary) FROM emp GROUP BY dept_id HAVING SUM (salary) > 200000