retrieve only second row of the table in oracle?

前端 未结 2 1534
心在旅途
心在旅途 2021-01-06 15:47

Can anyone help, how to retrieve exactly 2nd row from the table in oracle?

相关标签:
2条回答
  • 2021-01-06 16:20

    Thanks for your answers,now i found the solution for this,

          select * from
            (select rownum rn,column1,column2,...,columnn from tablename)
          where
             rn=2 
    

    Now you can check this and post your valuable comments.

    0 讨论(0)
  • 2021-01-06 16:21

    Since the rows in a table are inherently unordered, the concept of "first" and "second" requires that you specify some way of enforcing order (i.e. an ORDER BY clause). The simplest way to do this is to use an analytic function

    SELECT *
      FROM (SELECT a.*,
                   row_number() OVER (ORDER BY some_column) rn
              FROM your_table a)
     WHERE rn = 2;
    

    You could also use ROWNUM though that requires an additional level of nesting

    SELECT *
      FROM (SELECT b.*, rownum rn
              FROM (SELECT *
                      FROM your_table a
                     ORDER BY some_column) b
             WHERE rownum <= 2)
     WHERE rn > 1
    
    0 讨论(0)
提交回复
热议问题