求薪水在部门的平均薪水之上的雇员姓名和及薪水


如题:求薪水在部门的平均薪水之上的雇员姓名和及薪水


拆题:1  部门的平均薪水;2  求高于平均的雇员姓名和薪水


1  部门的平均薪水

select avg(sal) avgsal, deptno from emp group by deptno;


2  高于部门平均薪水的雇员姓名及薪水

此处须要进行关联查询,须要比对,薪水高于部门平均薪水的雇员姓名

select a.ename, a.sal
  from emp a, (select avg(sal) avgsal, deptno from emp group by deptno) b
 where a.sal > b.avgsal
   and a.deptno = b.deptno;


为了方便检验上面sql查询出结果的准确性。我们查询时给出部门编号,方便比对

select a.ename, a.sal, a.deptno
  from emp a, (select avg(sal) avgsal, deptno from emp group by deptno) b
 where a.sal > b.avgsal
   and a.deptno = b.deptno;



原文地址:https://www.cnblogs.com/zhchoutai/p/6888031.html