注:HAVING子句可以筛选成组后的各组数据,WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前。而 HAVING子句在聚合后对组记录进行筛选。
select deptno,sum(sal) from emp where sal>1200 group by deptno having sum(sal)>8500 order by deptno;
上述语句的执行顺序如下:
1 select 查询
2 where 过滤
3 group by 分组
4 聚合函数
5 having
6 order by排序