You can also do this using the temp table:
SELECT TOP 3 Name FROM PuppyNames ORDER BY NumberOfVotes DESC
becomes
CREATE TABLE #RowNumberTable ( RowNumber int IDENTITY (1,1), PuppyName varchar(MAX) ) INSERT #RowNumberTable (PuppyName) SELECT TOP 3 Name FROM PuppyNames ORDER BY NumberOfVotes DESC SELECT * from #RowNumberTable ORDER BY RowNumber DROP TABLE #RowNumberTable
If you notice, there will be a SELECT statement. It is just surrounded by things that make line numbers work.
source share