查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序

select empno,ename,sal,sal*12 
from emp 
order by sal asc;


查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序

select empno,ename,sal,sal*12 
from emp 
order by sal desc;


查询员工信息,按入职日期降序排序,使用列名

select empno,ename,sal,hiredate,sal*12 "年薪" 
from emp
order by hiredate desc;


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;

wKioL1fPF0bQLpX5AABCAPF3IRc916.png


查询员工信息,按佣金升序或降序排列,null值看成最大值

select * from emp order by comm desc;

wKiom1fPF6DgSemrAABLe8uXUic010.png

wKiom1fPGADDzF0EAABLxyovWcg229.png


查询员工信息,对有佣金的员工,按佣金降序排列,当order by 和 where 同时出现时,order by 在最后

select *
from emp
where comm is not null
order by comm desc;

wKiom1fPGErhIamVAAAioTXH5yA219.png


查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列

select *
from emp
order by sal desc,hiredate desc;
select *
from emp
order by sal desc,hiredate asc;

注意:只有当sal相同的情况下,hiredate排序才有作用


查询20号部门,且工资大于1500,按入职时间降序排列

select *
from emp
where (deptno=20) and (sal>1500)
order by hiredate desc;

wKiom1fPGQmAzXylAAAeeNuz97c501.png


下面的字符串'30'可以隐式转换为数字

select * from emp where deptno in (10,20,30,50,'30');

wKioL1fPGmvQxzGfAABDHNXFTQk025.png

select * from emp where deptno in (10,20,30,50,'a');

wKioL1fPGnjzZi9iAAAOyYvTm94432.png