以oracle的scott账户:找到员工表中薪水大于本部门平均薪水的员工为例
多表查询方式:
select e.empno, e.ename, e.sal, d.avgsal
from emp e, (select deptno, avg(sal) avgsal from emp group by deptno) d
where e.deptno = d.deptno
and e.sal > d.avgsal ;
执行结果
相关子查询方式:
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)
查询执行计划模板:
explain plan for #解释执行计划
执行计划 #sql语句块,即你查询的sql语句复制粘贴到这里即可
select * from table(dbms_xplan.display); #查询执行计划
多表查询执行计划:
explain plan for #解释执行计划
select e.empno, e.ename, e.sal, d.avgsal
from emp e, (select deptno, avg(sal) avgsal from emp group by deptno) d #sql语句
where e.deptno = d.deptno
and e.sal > d.avgsal ;
select * from table(dbms_xplan.display); #查询执行计划
返回结果:
相关子查询执行计划:
explain plan for #解释执行计划
select empno,
ename,
sal,
(select avg(sal) from emp where deptno = e.deptno) avgsal #sql语句
from emp e
where sal > (select avg(sal) from emp where deptno = e.deptno);
select * from table(dbms_xplan.display); #查询执行计划表
返回结果:
观察两者CPU使用情况,优劣一目了然。