Skip to content

Instantly share code, notes, and snippets.

@tdittmar
Created December 9, 2016 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdittmar/052ae4a9813950886995a7ba5aa6d74e to your computer and use it in GitHub Desktop.
Save tdittmar/052ae4a9813950886995a7ba5aa6d74e to your computer and use it in GitHub Desktop.
Fetch n-th row from the end of a table in T-SQL
declare @whichRow INT
set @whichRow = 5
select top 1 * from
(
select top (@whichRow) * from <thetable> where <whereclause> order by <orderbyclause> desc
) tmp
order by id asc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment