Hello
have a good day
is there a way to retrive top records by range , i mean
like this
"select top 15 to 20 record from Table name any condition"
i dont mean top 15 or top 20, i mean records from 15-20
take care
thanking u in advance
Bye

Dani AI

Generated

Short answer: yes — you can return rows 15–20, and the best method depends on your SQL Server version. ’s nested-TOP trick is a valid legacy hack, but there are clearer, more maintainable options below (and a faster pattern for big tables).

For SQL Server 2012+ use ORDER BY with OFFSET / FETCH. OFFSET is the number of rows to skip (zero‑based); FETCH returns the next N rows. Example to get rows 15–20 (6 rows starting at the 15th row):

SELECT *
FROM dbo.YourTable
ORDER BY SomeColumn -- must be deterministic
OFFSET 14 ROWS
FETCH NEXT 6 ROWS ONLY;

This OFFSET/FETCH syntax and its requirements are documented by Microsoft. (learn.microsoft.com)

On older servers (or when you prefer a window function) use ROW_NUMBER() to assign row numbers and filter by that number. A common pattern uses a CTE or derived table:

WITH Numbered AS (
  SELECT *, ROW_NUMBER() OVER (ORDER BY SomeColumn) AS rn
  FROM dbo.YourTable
)
SELECT *
FROM Numbered
WHERE rn BETWEEN 15 AND 20
ORDER BY rn;

ROW_NUMBER (a ranking/window function) is the standard way to implement stable pagination when OFFSET/FETCH isn’t available. (learn.microsoft.com)

Notes and tips:

  • Always include a deterministic ORDER BY (add a unique key as a tiebreaker) or pages can shift; Microsoft documents this requirement and shows using snapshot/serializable isolation if you need perfectly repeatable pages. (learn.microsoft.com)
  • For large offsets, OFFSET/FETCH (and LIMIT/OFFSET) can be slow because the engine must skip many rows. Use keyset (seek) pagination — e.g. WHERE id > @LastID ORDER BY id ASC LIMIT 6 / TOP — for much better, consistent performance on big tables. See the “no-offset” / keyset pagination guidance. (use-the-index-luke.com)

Use the method that fits your SQL Server version and performance needs.

Hey noman,

You can do this real easy in MySQL. Use the LIMIT and OFFSET keyword. Thats it you are done.

But in MSSQL there is no LIMIT and OFFSET function. Unfortunately there is no other in-build clause provided in MS SQL to do this.But the same feature can be achieved in MS SQL with a little trick.

Suppose you want to write a query to return values from 6 - 8 (6,7,8) ,this can be achieved by TOP clause in MSSQL.

This is very simple,
Results 6-8 would be the first 3 rows of a TOP 3 statement, with a nested query with a TOP 8 statement in the opposite order.

Consider the employees table in NorthWind database,

This query will fetch you rows from 6-8(6,7,8).

SELECT * FROM 	(SELECT TOP 3 * 
		FROM 	(SELECT TOP 8 * 
			FROM [Employees] 
			ORDER BY [EmployeeID]  ASC) AS tbl1 
		ORDER BY EmployeeID DESC) AS tbl2 
ORDER by EmployeeID ASC

Note:
1. Use the Alias tbl1,tbl2
2. Tweak the ORDER BY for complex results.

commented: great post, looked for exactly this! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.