相关子查询(由不相关子查询转换思想)

发布时间 2023-05-28 21:53:42作者: XieLumeng

查询本部门最高工资的员工信息:emp:员工信息表 deptno:部门号  sal:员工薪水

不相关其中几条:

select * from emp e where e.deptno=10 and sal =(select max(sal) from emp where deptno=10)

unino

select * from emp e where e.deptno=20 and sal =(select max(sal) from emp where deptno=20)

union

...

 

转换:相关子查询

select * from emp e where  sal =(select max(sal) from emp where deptno=e.deptno)

子查询不能单独运行:先运行外查询有了e表才能运行子查询