SQL
5 min read
BEGINNER
LIMIT and FETCH
Quick AnswerPagination returns a portion of an ordered result. LIMIT sets the maximum number of rows and OFFSET skips rows before returning them.
Core idea
Pagination returns a portion of an ordered result. LIMIT sets the maximum number of rows and OFFSET skips rows before returning them.
Minimal example
sql
SELECT id FROM products ORDER BY id LIMIT 2;Expected SQLite rows:
text
12How it works
PostgreSQL and SQLite support LIMIT 2 OFFSET 2. PostgreSQL also supports the standard-oriented OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY syntax after ORDER BY; SQLite does not support FETCH. Large offsets can require processing many skipped rows, and changes between requests can shift page boundaries.
Practical example
sql
SELECT id FROM products ORDER BY id LIMIT 2 OFFSET 2;Expected SQLite rows:
text
34Common mistake and practice
Always use a unique tie-breaker. Request the second page of two products ordered by id; expect IDs 3 and 4.