1.使用With 子句重用子查询
用于在sql多次使用相同的子查询时:
示例一、显示部门工资总和高于雇员工资总和三分之一的部门名及工资总和(两次相同子句)
SQL> select dname,sum(sal) as dept_total from emp,dept
where emp.deptno=dept.deptno group by dname
having sum(sal)>
(select sum(sal) *1/3 from emp,dept
where emp.empno=dept.empno);
示例二、显示部门工资总和高于雇员工资总和三分之一的部门名及工资总和(使用with子句重用子查询)
SQL> with summary as
(select dname,sum(sal) as dept_total from emp,dept
where emp.deptno=dept.deptno group by dname
)
select dname,dept_total from summary dept_total>
(select sum(dept_total)*1/3 from summary);
用于在sql多次使用相同的子查询时:
示例一、显示部门工资总和高于雇员工资总和三分之一的部门名及工资总和(两次相同子句)
SQL> select dname,sum(sal) as dept_total from emp,dept
where emp.deptno=dept.deptno group by dname
having sum(sal)>
(select sum(sal) *1/3 from emp,dept
where emp.empno=dept.empno);
示例二、显示部门工资总和高于雇员工资总和三分之一的部门名及工资总和(使用with子句重用子查询)
SQL> with summary as
(select dname,sum(sal) as dept_total from emp,dept
where emp.deptno=dept.deptno group by dname
)
select dname,dept_total from summary dept_total>
(select sum(dept_total)*1/3 from summary);