How do I select last 5 rows in a table without sorting?

前端 未结 22 1319
-上瘾入骨i
-上瘾入骨i 2020-11-30 04:37

I want to select the last 5 records from a table in SQL Server without arranging the table in ascending or descending order.

22条回答
  •  被撕碎了的回忆
    2020-11-30 05:24

    Well, the "last five rows" are actually the last five rows depending on your clustered index. Your clustered index, by definition, is the way that he rows are ordered. So you really can't get the "last five rows" without some order. You can, however, get the last five rows as it pertains to the clustered index.

    SELECT TOP 5 * FROM MyTable
    ORDER BY MyCLusteredIndexColumn1, MyCLusteredIndexColumnq, ..., MyCLusteredIndexColumnN DESC
    

提交回复
热议问题