首页 > 数据库 > Oracle > SQL面试题3:获取平均工资最高的三个部门
SQL面试题3:获取平均工资最高的三个部门
1 创建表格并输入数据
create table company(id int, name varchar2(10), salary number, deptno int);
insert into company values(101, 'Zheng', 5000, 1);
insert into company values(102, 'Zhang', 6000, 1);
insert into company values(103, 'Lin', 7000, 2);
insert into company values(104, 'Li', 8000, 2);
insert into company values(105, 'Chen', 9000, 3);
insert into company values(106, 'Gao', 4000, 3);
insert into company values(107, 'Liu', 3500, 4);
insert into company values(108, 'Chen', 10000, 4);
insert into company values(109, 'Sun', 15000, 5);
insert into company values(110, 'Huang', 25000, 5);
commi;
2 按部门排列平均工资
select deptno, avg(salary) from company group by deptno order by avg(salary) desc;
DEPTNO AVG(SALARY)
---------- -----------
5 20000
2 7500
4 6750
3 6500
1 5500
3 选取前三名
select * from (select deptno, avg(salary) from company group by deptno order by avg(salary) desc) where rownum<=3;
DEPTNO AVG(SALARY)
---------- -----------
5 20000
2 7500
4 6750
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29485627/viewspace-1850877/ ,如需转载,请注明出处,否则将追究法律责任。