建表和插入数据语句:
create table t
as
select object_name ename,
mod(object_id,50) deptno,
object_id sal
from all_objects
where rownum <= 1000
用相关子查询查出每个deptno里sal最高或者最低的3个人。
select *
from t t1
where t1.sal in (select sal
from (select distinct sal, deptno
from t b
where b.deptno = t1.deptno
order by b.deptno, b.sal desc) a
where rownum <= 3)
但用
select *
from t t1
where t1.sal in (select sal
from (select distinct sal, deptno
from t b
order by b.deptno, b.sal desc) a
where rownum <= 3 and b.deptno = t1.deptno )
确可以,请问这是什么原因?