0

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

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Letscode
1

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.

Votes + Comments
great post, looked for exactly this!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.