1、查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序
select empno,ename,sal,sal*12
from emp
order by sal asc;
2、查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序
select empno,ename,sal,sal*12
from emp
order by sal desc;
3、查询员工信息,按入职日期降序排序,使用列名
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by hiredate desc;
4、order by后面可以跟列名、别名、表达式、列号(从1开始,在select子句中的列号)
列名:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by hiredate desc;
别名:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by "年薪" desc;
表达式:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by sal*12 desc;
列号,从1开始:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by 5 desc;
5、查询员工信息,按佣金升序或降序排列,null值看成最大值
select * from emp order by comm desc;
6、查询员工信息,对有佣金的员工,按佣金降序排列,当order by 和 where 同时出现时,order by 在最后
select *
from emp
where comm is not null
order by comm desc;
7、查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列
select *
from emp
order by sal desc,hiredate desc;
select *
from emp
order by sal desc,hiredate asc;
注意:只有当sal相同的情况下,hiredate排序才有作用
8、查询20号部门,且工资大于1500,按入职时间降序排列
select *
from emp
where (deptno=20) and (sal>1500)
order by hiredate desc;
select * from emp where deptno in (10,20,30,50,'a');