CPCODELAB
SQL

LIMIT & OFFSET — Pagination

6 min read

LIMIT and OFFSET

LIMIT restricts how many rows are returned. OFFSET skips a number of rows before starting to return results. Together they implement pagination — a technique used in nearly every web application to avoid loading thousands of rows at once.

sql
-- First 10 students
SELECT name FROM students ORDER BY id LIMIT 10;

-- Second page of 10 (rows 11-20)
SELECT name FROM students ORDER BY id LIMIT 10 OFFSET 10;

-- Third page
SELECT name FROM students ORDER BY id LIMIT 10 OFFSET 20;

Always pair LIMIT with ORDER BY. Without a deterministic sort order, different pages may return duplicate or missing rows as underlying data changes.

Ready to test yourself?
Practice SQL— quiz & coding exercises