(02)找到员工表中工资最高的前三名
ROWNUM EMPNO ENAME SAL
------ ----- ---------- -------
1 7839 KING 5000
2 7788 SCOTT 3000
3 7902 FORD 3000
select rownum,empno,ename,sal
from (select * from emp order by sal desc)
where rownum<=3;
(03)找到员工表中薪水大于【本】部门平均薪水的员工
EMPNO ENAME SAL AVGSAL
----- ---------- ------- ----------
7499 ALLEN 1600 1566
7566 JONES 2975 2175
7698 BLAKE 2850 1566
7788 SCOTT 3000 2175
7839 KING 5000 2916
7902 FORD 3000 2175
select e.ename "姓名",xx.deptno "部门号",round(xx.avgsal,0) "部门平薪",e.sal "员工薪水",e.deptno "员工所在部门号"
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) xx
where (e.deptno=xx.deptno) and (e.sal>xx.avgsal);
(04)有一个员工表empinfo结构如下
create table empinfo(
fempno varchar2(10) not null primary key,
fempname varchar2(20) not null,
fage number(2) not null,
fsalary number(10,2) not null
);
insert into empinfo(fempno,fempname,fage,fsalary) values('1','AA',30,7000);
insert into empinfo(fempno,fempname,fage,fsalary) values('2','BB',31,8000);
insert into empinfo(fempno,fempname,fage,fsalary) values('3','CC',32,9000);
insert into empinfo(fempno,fempname,fage,fsalary) values('4','DD',33,10000);
insert into empinfo(fempno,fempname,fage,fsalary) values('5','EE',34,11000);
insert into empinfo(fempno,fempname,fage,fsalary) values('6','FF',35,12000);
insert into empinfo(fempno,fempname,fage,fsalary) values('7','GG',36,13000);
insert into empinfo(fempno,fempname,fage,fsalary) values('8','FF',37,14000);
假如该表有大约1000万条记录,写一条最高效的SQL语句,计算以下4种人中每种员工的数量
第1种人:fsalary>9999 and fage>35
第2种人:fsalary>9999 and fage<35
第3种人:fsalary<9999 and fage>35
第4种人:fsalary<9999 and fage<35
提示:只用一条SQL搞定
第1种人 第2种人 第3种人 第4种人
2 2 2 3
select
sum(case when e.fsalary>9999 and e.fage>35 then 1 else 0 end) "第1种人",
sum(case when e.fsalary>9999 and e.fage<35 then 1 else 0 end) "第2种人",
sum(case when e.fsalary<9999 and e.fage>35 then 1 else 0 end) "第3种人",
sum(case when e.fsalary<9999 and e.fage<35 then 1 else 0 end) "第4种人"
from empinfo e;