栗子:查询前三条记录
select e.*,rownum from emp e where rownum <= 3;
栗子:查询4到6条记录
select e.,rownum from emp e where rownum between 4 and 6; --错误的
--解决思路: 生成行号,作为表,再分页查询
select t. from (select e.*,rownum rn from emp e) t
where t.rn between 4 and 6;
查询工资最高的前三名
思路:先工资降序排序,再取前三名
-- 先排序,作为表,再生成行号
select e.,rownum from (select from emp order by sal desc) e
where rownum <= 3
查询工资最高的4到6名
思路:先降序排序,作为表,生成行号,作为表,再分页条件查询
select t.
from (select e.,rownum rn from (select * from emp order by sal desc) e) t
where t.rn between 4 and 6;