相关子查询能否用部门分组平均薪水来替代?
select rownum,empno,ename,sal from (select * from emp order by sal desc) where rownum <= 3;
8.一般先执行子查询,再执行主查询;但相关子查询例外
-- 查询在所在部门,工资高于平均工资的员工
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal > (select avg(sal) from emp where deptno=e.deptno);
*******************************************************************
SQL> select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
2 from emp e
3 where sal > (select avg(sal) from emp where deptno=e.deptno);
EMPNO ENAME SAL AVGSAL
---------- ---------- ---------- ----------
7499 ALLEN 1600 1566.66667
7566 JONES 2975 2175
7698 BLAKE 2850 1566.66667
7788 SCOTT 3000 2175
7839 KING 5000 2916.66667
7902 FORD 3000 2175
已选择6行。
deptn部门号需要与主查询的表中的员工的部门号保持一致,利用表别名的方式传递进来:
1.如上图:子查询一般先执行子查询,再执行主查询,相关子查询例外,上面的举例就是相关子查询,(截图是我为了这种用法,可以将select avg 放在筛选语句里。
相关子查询,我的理解就是在子查询中,通过表别名,将外面表关键字段等于子查询中字段,自己平时也经常用,应该清楚这个概念)
2.还有另一种方法可以查询(多表查询的方式),如下图:
3.explain plan for XXX查询语句;
执行完之后查询:select * from table(dbms_xplan.display);
总结:通过3的解释计划,相关子查询的方式消耗cpu资源较少,而多表查询这种消耗较大。
相关子查询:将主查询中的值作为参数传递给子查询
例:找到员工表中薪水大于本部门平均薪水的员工
1、 select empno,ename,sal,c.avgsal
from emp e
where sal> (select avg(sal) from emp where deptno=e.deptno) c;
2、
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal> (select avg(sal) from emp where deptno=e.deptno);
一般先执行子查询,再执行主查询
主查询和子查询的执行顺序:一般是先子查询再主查询
本质上来说还是先执行了子查询,只不过顺序是 子-主-子而已
原来别名还能做为子表传参影响执行顺序
主查询和子查询的执行顺序
1、一般先执行子查询,再执行主查询;但相关子查询例外
找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal> (select avg(sal) from emp where deptno=e.deptno);
相关子查询
找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal> (select avg(sal) from emp where deptno=e.deptno);
找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal> (select avg(sal) from emp where deptno=e.deptno);
相关子查询,是先执行主查询,然后执行子查询。
找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
相关子查询:查询员工表中薪水大于本部门平均薪水的员工
select empno ,ename,sal,(select avg(sal) from emp where deptno=e.deptno)avg
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
8.一般先执行子查询,再执行主查询;但相关子查询例外
(1)**相关子查询 (子查询使用了主查询字段)
select 员工号,姓名,工资,
(select avg(工资) from 员工表 where 部门号 = t1.部门号) 部门平均工资
from 员工表 t1
where 工资 > (select avg(工资)
from 员工表
where 部门号 = t1.部门号)
--赵春各部门薪资大于平均薪资的员工 (相关子查询)
select e.*, (select avg(sal) from emp where deptno=e.deptno) avgSal
from emp e
where e.sal>(select avg(sal) from emp where deptno=e.deptno)
select empno,ename,sal,(select avg(sal) from emp where deptno = e.deptno) avgsal from emp e where sal > (select avg(sal) from emp where deptno = e.deptno);
相关子查询:主表中的某个字段作为参数传入到子查询中
父查询使用别名