select a.员工姓名
from emp a,(select 部门,avg(工资) as avg_gz from emp group by 部门) as b
where a.部门=b.部门 and a.工资>b.avg_gz
select emp.*, b.pjsal from emp,(select emp.* ,a.pjsal from emp,(select avg(sal) pjsal,deptno from emp group by deptno) a where a.deptno=emp.deptno) b where emp.sal>b.pjsal and emp.empno=b.empno;
首先用select emp.* ,a.pjsal from emp,(select avg(sal) pjsal,deptno from emp group by deptno)a where a.deptno=emp.deptno查出每个部门的平均工资
然后使得每个人的工资和平均工资比较
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO PJSAL
----- ---------- --------- ----- ----------- --------- --------- ------ ----------
7839 KING PRESIDENT 1981-11-17 5000.00 10 2916.66666
7902 FORD ANALYST 7566 1981-12-3 3000.00 20 2175
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20 2175
7566 JONES MANAGER 7839 1981-4-2 2975.00 20 2175
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30 1566.66666
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30 1566.66666
select * from ( select emp.*, avg(emp.sal) over (partition by emp.deptid) as avgsal from emp) a where a.sal>a.avgsal order by emp.deptid; ~