SQL Flow Field Guide

Read one ordered page

Use OFFSET and FETCH to return a page after sorting the full result.

What you are trying to do

A request list shows two rows per page. The learner needs page two from four requests.

Work through it

  1. Read the unique QueueNumber sort key.
  2. Skip the first ordered row with OFFSET.
  3. Take the next two rows with FETCH.

Check your work

The result contains RequestId 20 and 30 with queue numbers 2 and 3. Request 40 is skipped and request 10 is on the next page.

Practice SQL

Open in SQL Flow
SELECT RequestId, QueueNumber
FROM (VALUES (40, 1), (20, 2), (30, 3), (10, 4)) AS RequestQueue(RequestId, QueueNumber)
ORDER BY QueueNumber
OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY;

Knowledge check

Commands in this lesson

Next lesson Name a row's grain and key