查询各部门中高于部门平均工资的人员,人数及该部门的平均工资

dept表deptno,deptname

emp表empno,empname,salary,deptno

先通过查询得到平均工资视图,然后在联结查询平均工资表和员工表,Group by修饰count(*),Group by要在where子句之后,order by子句之前。

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

 高于平均工资的人数

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

 

转载于:https://www.cnblogs.com/masterlibin/p/5641664.html

你可能感兴趣的:(查询各部门中高于部门平均工资的人员,人数及该部门的平均工资)