HAVING 子句

筛选 GROUP BY 子句的结果。根据语义,HAVING 子句必须发生在 GROUP BY 操作之后。该子句已添加到 SQL 标准,因为 WHERE 子句无法指定聚合函数

语法

HAVING condition[,...]

参数

condition
明确引用分组列,除非引用显示在聚合函数中。

示例

以下示例返回薪资大于 $800,000 的员工:


=> SELECT employee_last_name, MAX(annual_salary) as highest_salary FROM employee_dimension
     GROUP BY employee_last_name HAVING MAX(annual_salary) > 800000 ORDER BY highest_salary DESC;
 employee_last_name | highest_salary
--------------------+----------------
 Sanchez            |         992363
 Vogel              |         983634
 Vu                 |         977716
 Lewis              |         957949
 Taylor             |         953373
 King               |         937765
 Gauthier           |         927335
 Garnett            |         903104
 Bauer              |         901181
 Jones              |         885395
 Rodriguez          |         861647
 Young              |         846657
 Greenwood          |         837543
 Overstreet         |         831317
 Garcia             |         811231
(15 rows)