先上例了:
select job as "JOB1", avg(sal) as "avg sal" from scott.emp
group by "JOB"
having avg(sal) > 1500 and "JOB1" = 'MANAGER'
order by "avg sal"
;
-- 执行报错,ORA-00904:"JOB1":标识符无效
select job as "JOB1", avg(sal) as avg_sal from scott.emp
group by job
having avg(sal) > 1500 and job = 'MANAGER'and DEPTNO=20
order by 2;
-- 执行报错,ORA--00979:不是GROUP BY表达式
oracle执行顺序:先过滤一般条件(where), 再进行分组(group),然后对分组执行组函数,同时进行组函数过滤(having),最后对得到的数据排序显示。
注意:
- where 分组前过滤、having是分组后过滤;
- having通过过滤分组函数结果来筛选内容;
- having也不可以接分组函数的别名;
- having中的条件必须是group by的表达式,即在分组中出现过;
- having和group by都不可以接列的别名
- having是对group分组后的内容进行过滤,group不能接列别名、having也不能接列别名