Concept Flow - LIMIT and OFFSET for pagination
Start Query
Apply OFFSET
Skip OFFSET rows
Apply LIMIT
Return LIMIT rows
End Query
The query starts, skips rows based on OFFSET, then returns a limited number of rows based on LIMIT.
SELECT * FROM employees ORDER BY id LIMIT 3 OFFSET 2;
| Step | Action | Rows Considered | Rows Skipped (OFFSET) | Rows Returned (LIMIT) |
|---|---|---|---|---|
| 1 | Start query execution | All rows in employees ordered by id | 0 | 0 |
| 2 | Apply OFFSET 2 | All rows | 2 rows skipped | 0 |
| 3 | Apply LIMIT 3 | Remaining rows after offset | 2 | 3 rows selected |
| 4 | Return result | 3 rows | 2 skipped | 3 returned |
| 5 | End query | Result sent to client | 2 skipped | 3 returned |
| Variable | Start | After OFFSET | After LIMIT | Final |
|---|---|---|---|---|
| Rows considered | All rows ordered by id | All rows minus first 2 | 3 rows selected | 3 rows returned |
| Rows skipped | 0 | 2 | 2 | 2 |
| Rows returned | 0 | 0 | 3 | 3 |
LIMIT and OFFSET control pagination in SQL. OFFSET skips a number of rows first. LIMIT sets how many rows to return after skipping. Syntax: SELECT ... LIMIT count OFFSET skip; Use ORDER BY to ensure consistent row order.