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;