列出各个部门中工资高于本部门的平均工资的员工数和部门号

列出各个部门中工资高于本部门的平均工资的员工数和部门号,并按部门号排序。用SQL语句详细介绍一下

采纳率:51%11级2013.11.17

select a.deptno,count(*) from emp as a,
(select deptno,avg(sal) as avgsal from emp group by deptno) as b
where a.deptno=b.deptno
and a.sal>b.avgsal
group by a.deptno
order by a.deptno

表名为emp,deptno为部门号
(select deptno,avg(sal) as avgsal from emp group by deptno) as b
这个是查询每个部门的平均工资,并把这个结果集命名为b
然后关联emp表查询
where a.deptno=b.deptno
and a.sal>b.avgsal
这个就是在部门号相同的情况下,查找工资大于平均公司的人
最后count(*) 就是总人数,排序就正常order by 就OK

猜你喜欢

转载自blog.csdn.net/qq_41040268/article/details/82529448