-- Assume we have a table called Products with columns Id, Name, and Price
-- Assume we want to order the records by Price in descending order
-- Assume we want to fetch the records for page number @PageNum
-- Declare a variable for the page number
DECLARE @PageNum INT = 1 -- Change this value as needed
-- Declare a variable for the page size
DECLARE @PageSize INT = 20
-- Use OFFSET and FETCH clauses to skip and limit the number of records
SELECT Id, Name, Price
FROM Products
ORDER BY Price DESC
OFFSET (@PageNum - 1) * @PageSize ROWS -- Skip the previous pages
FETCH NEXT @PageSize ROWS ONLY -- Limit the current page
Tags
sql