来写个sql 玩玩。。
看到的一个题目,看下CSDN有多少开发达人。
要求:
1.不用order by求出emp表中的工资最高的前五名。
2.用1条sql实现
sql
------解决方案--------------------好有难度,同求一下,看看达人们的答案
------解决方案--------------------select /*+ index_desc(emp,ind_emp_sal)*/ from emp where rownum <= 5
ind_emp_sal是建立在emp(sal)上的索引.
------解决方案--------------------不用order by 用group by 行么,b字段是工资
select *
from temp t1
where t1.b >= (select max(b)
from (select t.b from temp t group by t.b)
where rownum <= (select count(*) from temp) - 4);
------解决方案--------------------
select *
from emp
start with sal = (select max(sal) from emp)
CONNECT BY PRIOR sal > sal and rownum <6
不过有两个sal 相等的 没取到
------解决方案--------------------select *
from (select (select count(*)
from emp tt
where tt.sal > t.sal) as cou,
t.sal
from emp t)
where cou < 5
and rownum <= 5
------解决方案--------------------select
sal
,sal_max
,rn
from
(select
sal
,rownum rn
,cal_cnt
,sal_max
from (select
sal
,sum(1) over(partition by 'a') cal_cnt
,max(sal) over(partition by 'a') sal_max
from emp
) bb
)
where rn < cal_cnt - 5