OFFSET FETCH — Complete Guide
OFFSET FETCH — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 18 of 100
OFFSET FETCH
SQL basics → Queries → Advanced
SQL basics · 1 — SELECT · ~6 min · SQL — SQL Queries & Clauses
What is this?
OFFSET … FETCH NEXT is SQL Server’s pagination syntax. Skip N rows, then take the next M. Requires ORDER BY.
Why should you care?
Page 2 of products in a shop UI needs skip/take — OFFSET FETCH does that in T-SQL.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
DECLARE @Page INT = 2, @PageSize INT = 2;
SELECT OrderId, City, Amount
FROM dbo.Orders
ORDER BY OrderId
OFFSET (@Page - 1) * @PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY;
What happened?
- Page 2 with size 2 skips the first two OrderId values and returns the next two.
- Variables make pagination reusable from an API.
Practice next
- Ensure at least four orders exist.
- Run with @Page = 1 and @Page = 2.
- Change @PageSize to 3.
- Page products by ProductId with page size 10.
- Return page metadata using COUNT(*) OVER() in a separate query.
Remember
OFFSET skips; FETCH takes the next page. ORDER BY is mandatory. Good default for simple API paging.
Catalog page API
GET /orders?page=3&size=20 uses OFFSET FETCH on DataVerse.
Outcome: Mobile app scrolls without downloading all history.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!