How to select a row based on its row number?

前端 未结 6 1472
猫巷女王i
猫巷女王i 2020-12-28 18:12

I\'m working on a small project in which I\'ll need to select a record from a temporary table based on the actual row number of the record.

How can I select a record

6条回答
  •  伪装坚强ぢ
    2020-12-28 18:29

    There are 3 ways of doing this.

    Suppose u have an employee table with the columns as emp_id, emp_name, salary. You need the top 10 employees who has highest salary.

    1. Using row_number() analytic function

      Select * from
      ( select emp_id,emp_name,row_number() over (order by salary desc) rank
      from employee)
      where rank<=10
      
    2. Using rank() analytic function

      Select * from
      ( select emp_id,emp_name,rank() over (order by salary desc) rank
      from employee)
      where rank<=10
      
    3. Using rownum

      select * from
      (select * from employee order by salary desc)
      where rownum<=10;
      

提交回复
热议问题