| | |
Range of records in Top clause
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
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).
Note:
1. Use the Alias tbl1,tbl2
2. Tweak the ORDER BY for complex results.
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).
MS SQL Syntax (Toggle Plain Text)
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.
Save White Tiger
![]() |
Similar Threads
- [JOB] Software Engineer - C++ (Based in Singapore) (Software Development Job Offers)
- How to counta repeat records (Visual Basic 4 / 5 / 6)
- can i select the 2nd row/record from a table (MySQL)
- Evolution: Science vs. Religion (Geeks' Lounge)
- Trouble Reading Records (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- purpose of exception handling 'finally' clause (Java)
Other Threads in the MS SQL Forum
- Previous Thread: need help optimizing
- Next Thread: effective user database
| Thread Tools | Search this Thread |





