HAVING Clause
Restricts the results of a GROUP BY Clause.
Syntax
HAVING condition [, ...]
Parameters
condition |
Must unambiguously reference a grouping column, unless the reference appears within an aggregate function |
Notes
- Semantically, the HAVING clause occurs after the GROUP BY operation.
- You can use expressions in the HAVING clause.
- The HAVING clause was added to the SQL standard because you cannot use WHERE with Aggregate Functions.
Example
The following example returns the employees with salaries greater than $50,000:
=> SELECT employee_last_name, MAX(annual_salary) as "highest_salary" FROM employee_dimension GROUP BY employee_last_name HAVING MAX(annual_salary) > 50000; employee_last_name | highest_salary --------------------+---------------- Bauer | 920149 Brown | 569079 Campbell | 649998 Carcetti | 195175 Dobisz | 840902 Farmer | 804890 Fortin | 481490 Garcia | 811231 Garnett | 963104 Gauthier | 927335 (10 rows)