SQL面試題3:獲取平均工資最高的三個部門

zhenghaishu發表於2015-12-01

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部落格 ” ,連結:http://blog.itpub.net/29485627/viewspace-1850877/,如需轉載,請註明出處,否則將追究法律責任。

相關文章