Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do LIMIT and OFFSET work together to show one page of results?

Quick answer

In SQLite, use ORDER BY to define the row order, LIMIT to set how many rows to return, and OFFSET to skip rows before returning them. For example, with five titles sorted alphabetically, LIMIT 2 OFFSET 2 returns the third and fourth titles. For page size two, successive offsets are 0, 2, and 4. Add a unique tie-breaker when sort values can match.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a catalog and want just one page of results in SQLite. What do LIMIT and OFFSET each do?

Geez

Think of the query as three steps: first put the results in a defined order, then skip some rows, then take a limited number. ORDER BY defines the order; OFFSET says how many rows to skip; LIMIT says how many rows to return after skipping. SQLite documents that LIMIT N OFFSET M skips the first M rows of the ordered results and returns the next N, or fewer if fewer remain. [S1]

Here is a complete, invented catalog example you can run in SQLite:

CREATE TABLE catalog (
  id INTEGER PRIMARY KEY,
  title TEXT NOT NULL
);

INSERT INTO catalog (id, title) VALUES
  (1, 'Apple'),
  (2, 'Birch'),
  (3, 'Cedar'),
  (4, 'Dune'),
  (5, 'Elm');

SELECT title
FROM catalog
ORDER BY title
LIMIT 2 OFFSET 2;

Exact output:

Cedar
Dune

The titles sort as Apple, Birch, Cedar, Dune, Elm. The offset skips Apple and Birch; the limit takes the next two. The catalog and output are illustrative, not a claim about any real dataset.

Follow-up question

Why does the ORDER BY matter? Can't I just use LIMIT 2 OFFSET 2?

Geez

Without ORDER BY, SQLite does not define the order of multiple returned rows. [S1] That means “skip two, then take two” would not reliably mean “show the third and fourth titles alphabetically.” Choose the order that makes sense for your page—such as title, date, or another field—and state it in the query.

If two rows have the same sort value, specify a further tie-breaker, preferably a unique column such as an ID. For example:

SELECT id, title
FROM catalog
ORDER BY title, id
LIMIT 2 OFFSET 2;

This makes the ordering rule explicit when titles match. The source supports the need for a tie-breaker to define which tied rows come first; it does not establish that pages remain identical if the catalog changes between separate page queries. If rows are added, removed, or edited between requests, the page contents may differ.

Follow-up question

If my page size is two, how do I calculate the offset for each page?

Geez

For pages numbered from 1, calculate the offset as (page number - 1) × page size. With a page size of two:

  1. Page 1: LIMIT 2 OFFSET 0 returns Apple and Birch.
  2. Page 2: LIMIT 2 OFFSET 2 returns Cedar and Dune.
  3. Page 3: LIMIT 2 OFFSET 4 returns Elm, because only one row remains.

SQLite returns fewer than the limit when fewer rows remain. [S1] If your application numbers pages starting at 0 instead, the offset is page number × page size. Keep the numbering convention clear so you do not accidentally skip the first page.

Follow-up question

Can you give me a reusable query pattern and a quick way to check that I got the intended page?

Geez

Use this pattern, replacing the column names and page values with your own:

SELECT id, title
FROM catalog
ORDER BY title, id
LIMIT 2 OFFSET 2;

Before relying on a page, check that the ordering expresses your intended sequence, that the tie-breaker resolves equal sort values, and that the offset matches your page-number convention. Run the same query on a small, known set of rows and count from the start of its ordered output: rows before the offset should be skipped, and no more than the limit should appear. Those checks verify the query’s intended slice for that data; they do not guarantee that results stay fixed if the catalog changes between page queries. The cited behavior here is SQLite-specific, so do not assume another database uses identical syntax or behavior. [S1]

A practical checklist

  • Choose the column or columns that define the desired row order.

  • Add a unique tie-breaker if sort values can be equal.

  • Set LIMIT to the number of rows you want per page.

  • Calculate OFFSET from your page size and whether page numbering starts at 0 or 1.

  • Test against a small, known ordered result and confirm which rows are skipped and returned.

  • Remember that changes to the catalog between page queries can change what appears on a page.

A few more useful answers

What happens when the offset goes past the last row?

SQLite returns fewer than the limit when fewer rows remain; if no rows remain, the query returns no rows. [S1]

Does LIMIT set the total number of rows in the catalog?

No. LIMIT sets the maximum number returned by that query after the offset is applied. [S1]

Does the example establish how pagination works in every database?

No. The documented behavior and syntax here are for SQLite; the cited material does not establish behavior for other database systems.

Sources

References used for this guide.

  1. [S1] SQLite SELECT — ORDER BY and LIMIT clauses
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work