使用offset子句进行前n个查询

时间:2018-03-13 10:49:50

标签: sql oracle

SELECT last_name 
FROM employees
order by last_name DESC
fetch first 5 rows only;

我是SQL的初学者。我正在尝试运行此查询但仍然收到错误

  

sql命令未正确结束

我做错了什么?

1 个答案:

答案 0 :(得分:1)

oracle版本< 12

  select * from 
        (select last_name, row_number() over(order by last_name desc nulls last) rnm
           from employees) 
   where rnm<=5;

oracle版本12

SELECT last_name 
  FROM employees
 ORDER BY last_name DESC
OFFSET 1 ROWS FETCH NEXT 5 ROWS ONLY;