Skip to content

Instantly share code, notes, and snippets.

@tdittmar
Created December 9, 2016 09:06
Show Gist options
  • Select an option

  • Save tdittmar/052ae4a9813950886995a7ba5aa6d74e to your computer and use it in GitHub Desktop.

Select an option

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