Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server OFFSET equivalent

I am using SQL Server 2008 Enterprise on Windows Server 2008. I want to select result from top 11 to top 20 (e.g. I am only interested in the 11th to the 20th result). Any ideas how to write this query in tsql efficiently?

thanks in advance, George

like image 375
George2 Avatar asked Jul 10 '10 13:07

George2


2 Answers

Unfortunately SQL Server does not offer anything similar to MySQL's OFFSET syntax. However, you may want to try using a derived table as follows:

SELECT some_field
FROM   (
          SELECT some_field, ROW_NUMBER() OVER (ORDER BY some_id) AS rownum
          FROM   table
       ) AS t
WHERE  t.rownum BETWEEN 11 AND 20
like image 158
Daniel Vassallo Avatar answered Sep 30 '22 20:09

Daniel Vassallo


See following solution is applicable only for SQL Server 2012 onwards.

Limit with offset in sql server:

SELECT email FROM myTable
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;

//offset - no. of skipped rows

//next - required no. of next rows

like image 20
Somnath Muluk Avatar answered Sep 30 '22 20:09

Somnath Muluk