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

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.