帮个忙 行么! 能告诉我下面 sql 执行的顺序么! 是先执行()里面的还是外的 sql语句的目的是求(每个部门最高工资人的信息)
select ename, deptno
from emp e
where sal = (select max(sal)
from emp
where deptno = e.deptno)
select ename, deptno
from emp e
where sal = (select max(sal)
from emp
where deptno = e.deptno)
from emp
这样不行?
from emp e 然后再在过滤where sal = (select max(sal)
from emp
where deptno = e.deptno)
2、2的SQL语句貌似行不通,楼主要的是每个部门最高工资人的信息,但2楼的SQL语句需要Group by ename, deptno,这样其实是把每个部门相同名字的人的最高工资的那个人的信息检索出来。3、我这里也提供了一个可以同样达到目的的SQL语句:
select *
from emp a
where (a.empno, a.sal) =
(select t.empno, max(t.sal) from emp t group by t.empno)
执行时还是顺序还是一样。
只是在where条件比较的时候会和子查询返回的结果比较。
即子查询执行完才进行比较;满足条件的将返回。
from emp e
where sal = (select max(sal)
from emp)
这样就可以了
from emp e
where sal = (select max(sal)
from emp
where deptno = e.deptno) 因为红色部分 所以这是个关联子查询 子查询中用到了父查询中的条件
外层的查询先执行
先从外层取出一条记录 以这条记录的deptno值为条件去执行内部查询 选出这个部门的最高工资 接着把这个最高工资数返回给外部查询当做where条件 这样外部查询就可以查询出相关人员的ename和deptno相关信息了
接下来再从外部循环中取出第二条记录……就这样一直循环下去