select empno,ename,sal,rownum from emp;
select * from (select * from emp order by sal desc) where rownum<=5;
select * from (select ename,sal,row_number() over(order by sal desc) as num from emp) where num<=5;
select * from (select ename,sal,row_number() over(order by sal desc) from emp) where rownum<=5
--工资的前3名
select * from emp where sal >=any(select * from (select sal from emp order by sal desc) where rownum<=3);
select * from(select * from emp order by sal desc) where rownum <4;
select * from (select ename,sal,empno,deptno ,row_number() over (order by sal desc) from emp) where rownum<4;
select * from (select ename,sal,empno,deptno ,row_number() over (order by sal desc) as num from emp) where num<4
--按照工资排序,取出第6名到第10名
--使用伪列获得
select * from (select ename,sal,rownum r from (select * from emp order by sal desc) where rownum<=10) where r>5;
--使用排名函数获得
select * from (select ename,sal,row_number() over(order by sal desc) as num from emp) where num>5 and num<=10;
------- 按工资从高到低获得工资排名第四的员工
select * from (select ename,sal,row_number() over(order by sal desc) as num from emp) where num=4;
select * from (select ename,sal,rownum r from (select * from emp order by sal desc) where rownum<=4) where r=4;
总结oracle中rownum和row_number()的区别
row_number()是分析函数,基本语法为row_number() over(partition by 字段 order by 字段) rownum是一个伪列
select * from dept where rownum<=3;
select * from dept where rownum between 2 and 3;这儿会出错,因为rownum的特性(没有1就不会有2,没有3)决定的
SELECT * FROM (SELECT A.*,ROWNUN FROM DEPT A)T1 WHERE T1.ROWNUM BETWEEN 2 AND 3;这么写不对,要这样写
SELECT * FROM (SELECT A.*,ROWNUM RN FROM DEPT A)T1 WHERE T1.RN BETWEEN 2 AND 3;