-1

How can I get Records from 10-to15 I find some code in net Like

Select * 
from Employee 
LIMIT 10 to 15 

but I am getting an error at Limit

2 Answers2

1

You can use offset and fetch next:

select e.*
from Employee e
order by ??
offset 9
fetch next 6 rows only;

Note that fetch is available from SQL Server 2012 onward.

Normally, you do this with an order by. The ?? is for the column/expression for ordering. Fetching with an offset doesn't make sense without an order by, because result sets are in an arbitrary order unless the ordering is explicit.

In earlier (supported) versions, I would recommend row_number():

select e.*
from (select e.*, row_number() over (order by ??) as seqnum
      from Employee e
     ) e
where seqnum between 10 and 15;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Here wat is t. Error as Incorrect syntax near the keyword 'fetch'. Msg 153, Level 15, State 2, Line 5 Invalid usage of the option first in the FETCH statement. –  Jun 25 '17 at 16:17
0

alternatively, you can use "ROW_NUMBER()". See the following code sample from this stack overflow: Row Offset in SQL Server

SELECT col1, col2 
 FROM (
 SELECT col1, col2, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum
 FROM MyTable
 ) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN @startRow AND @endRow
AlexT82
  • 1,114
  • 1
  • 7
  • 12