Posts Select Top N Rows from a table in Sql Server
Post
Cancel

Select Top N Rows from a table in Sql Server

Selecting Top 10 rows is easy. Just do

1
SELECT TOP 10 * FROM PERSON

But what if the number of rows you want to get is dynamic? What if you have a variable N for the number of rows to return?
Try this

1
2
3
4
5
6
7
8
DECLARE @count INT = 5

SELECT *
FROM <<Table>>
WHERE <condition>
ORDER BY <<column>>
OFFSET 0 ROWS
FETCH FIRST @count ROWS ONLY

Hope this helped.

This post is licensed under CC BY 4.0 by the author.