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

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;

 

posted @ 2016-07-04 20:43  32ddd  阅读(3215)  评论(0编辑  收藏  举报