sqlcmd.net validated sql reference
intermediate pagination MySQL MariaDB SQL Server PostgreSQL SQLite

Paginate With Offset

Skip earlier rows and return the next slice of a sorted result set.

Docker-validated Not currently validation-green

Return the second page of two rows

This example skips the first two sorted rows and returns the next two.

MySQL MariaDB PostgreSQL SQLite
Engine-specific syntax
Setup
CREATE TABLE posts (id INT, title VARCHAR(50));

INSERT INTO
  posts (id, title)
VALUES
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta'),
  (5, 'Echo');
SQL
SELECT
  id,
  title
FROM
  posts
ORDER BY
  id
LIMIT
  2
OFFSET
  2;
idtitle
3Charlie
4Delta
SQL Server
Engine-specific syntax
Setup
CREATE TABLE posts (id INT, title VARCHAR(50));

INSERT INTO
  posts (id, title)
VALUES
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta'),
  (5, 'Echo');
SQL
SELECT
  id,
  title
FROM
  posts
ORDER BY
  id
OFFSET
  2 ROWS
FETCH NEXT
  2 ROWS ONLY;
idtitle
3Charlie
4Delta

The page contents match, but the pagination syntax differs materially on SQL Server.

Where this command helps.

  • serving page 2 or later in a table UI
  • walking through large ordered result sets in fixed-size slices

What the command is doing.

Offset-based pagination returns one page of rows at a time from an ordered result set. The syntax differs across engines, so this concept is best documented with per-engine SQL while keeping the expected page contents aligned.