Introduction
SQL Server 2012 has introduced two new keywords for doing pagination of the result sets: OFFSET and FETCH. OFFSET provides a starting row from which SQL Server needs to start fetching rows and FETCH provides the number of rows we want to fetch from the result set (or in a query). OFFSET and FETCH can be used only with an order by clause.
Syntax
[ORDER BY { order by expression [ASC|DESC] } [ ,...n][OFFSET offset row count [ROW |ROWS] FETCH FIRST | NEXT fetch row count [ROW |ROWS] ONLY] ]
Offset row count: It is the number of rows to skip. This parameter must be an integer and greater than or equal to zero.
Fetch row count: It is the number of rows to return. This parameter must be an integer and greater than or equal to one.
Example
Suppose I have a table containing the yearly profits of the company. This table contains more than 50 rows. Now from my application I just want to show only 10 records at a time, so I need to use pagination.
--Create Table and Insert some dummy data.
CREATE TABLE #YearWiseBusinessData
(
Year INT,
Profit MONEY NOT NULL
)
DECLARE @year INT = 1960
DECLARE @profit money = 2300.00
WHILE (@year <=2013)
BEGIN
INSERT INTO #YearWiseBusinessData VALUES(@year,@profit)






Join the conversation! Your thoughts help the community grow.