How to select the nth row in a SQL database table?

后端 未结 30 2888
执笔经年
执笔经年 2020-11-22 06:06

I\'m interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can b

30条回答
  •  日久生厌
    2020-11-22 06:43

    I suspect this is wildly inefficient but is quite a simple approach, which worked on a small dataset that I tried it on.

    select top 1 field
    from table
    where field in (select top 5 field from table order by field asc)
    order by field desc
    

    This would get the 5th item, change the second top number to get a different nth item

    SQL server only (I think) but should work on older versions that do not support ROW_NUMBER().

提交回复
热议问题