員工部門工資SQL面試題

員工部門工資SQL面試題

現有employee 表,表中有 員工編號(id) 員工年齡(age) 員工工資(salary) 員工部門(deptid), 按要求用一條SQL語句完成

create table employee( 
id int  identity(1,1) primary key , 
name varchar(50), 
salary bigint, 
deptid int);

1.查出每個部門高於部門平均工資的員工名單

select ta.* from employee ta, 
(select deptid,avg(salary) avgsal from employee group by deptid)tb  
where ta.deptid=tb.deptid and ta.salary>tb.avgsal

2、列出各個部門中工資高於本部門的平均工資的員工數和部門號,並按部門號排序。

select ta.deptid,count(*) as ‘人數’  from employee ta, 
(select deptid,avg(salary) avgsal from employee group by deptid)tb  
where ta.deptid=tb.deptid and ta.salary>tb.avgsal group by ta.deptid order by ta.deptid

3.求每個部門工資不小於6000的人員的平均值;

SELECT avg(salary) as ‘平均值’,deptid FROM employee  where salary >=6000 GROUP BY dept_id

4、各部門在各年齡段的平均工資

select deptid,
sum(case when age < 20 then salary else 0 end) / sum(case when age <20 then 1 else 0 end) as “20歲以下平均工資”,
sum(case when age >= 20 and age <40 then salary else 0 end) / sum(case when age >= 20 and age <40 then 1 else 0 end) as “20至40歲平均工資”,
sum(case when age >= 40 then salary else 0 end) / sum(case when age >=40 then 1 else 0 end) as “>40歲及以上平均工資”,
from employee
group by deptid

以上SQL面試題,經常會出現在筆試環節,特別是java開發工程師崗位,雖然並不是很難,但對於那些比較熟悉SSH開發,不經常寫SQL的同學來說,有時候還真是不知道怎麼寫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章