Oracle条件查询

--查询各个部门的编号,最高工资,总和,平均工资
--并且按照个部门的总工资进行降序排列
select deptno,max(sal),sum(sal),avg(sal)
from teacher
group by deptno
order by sum(sal) desc
--再增加一个条件 并且 部门人数在10人以上的
select deptno,max(sal),sum(sal),avg(sal)
from teacher
group by deptno
having count(deptno)>20
order by sum(sal) desc


--nvl(E1,E2):如果E1为空,则返回E2,否则返回E1
select nvl(mgrno,0) from teacher
--nvl2(E1,E2,E3):如果E1为空,则返回E3,否则返回E2
select nvl2(mgrno,1000,0) from teacher
--decode(value,表达式1,表达式1对应的值....) 底层就是一个if else if
select decode(mgrno,null,0,2000) from teacher

sql语句的执行顺序
1.from 表
2.where 条件
3.group by 分组 根据列
4.having 分组条件
5.select 01 .列 02.distinct 03.top
6.order by 根据指定的列 进行排序

  (以上内容来自王硕老师)

原文地址:https://www.cnblogs.com/liu-chao-feng/p/5890335.html