员工部门工资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的同学来说,有时候还真是不知道怎么写。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值