先说说 :
sql的编写顺序
select .. from .. where .. group by ..having .. order by ..
sql的执行顺序
1. 先执行外层查询
sql的编写顺序
select .. from .. where .. group by ..having .. order by ..
sql的执行顺序
from .. where .. group by .. having .. select .. order by ..
关联子查询 :
在关联子查询中,对于外部查询返回的每一行数据,内部查询都要执行一次。另外,在关联子查询中是信息流是双向的。外部查询的每行数据传递一个值给子查询,然后子查询为每一行数据执行一次并返回它的记录。然后,外部查询根据返回的记录做出决策。
select * from dept d where exists(select * from emp e where e.deptno = d.deptno);
1. 先执行外层查询
2. 再执行内层查询
非关联子查询:非相关子查询是独立于外部查询的子查询,子查询执行完毕后将值传递给外部查询
select * from emp where sal = (select max(sal) from emp);
特点
1. 先执行内层查询
2. 再执行外层查询