OFFSET
- SQL course
- Beginner
- Lesson 17/115
- 4 min
Skip rows and paginate with OFFSET.
OFFSET
With OFFSET you skip the first N rows and return the rest. Combined with LIMIT it is used for pagination.
SELECT name, price
FROM products
ORDER BY price
LIMIT 5 OFFSET 10;This returns rows 11 through 15; like the third page.
name, price is selected from products. The result is sorted by price in ascending order. 10 rows are skipped and the next 5 are returned.
OFFSET — details
If you show 5 rows per page, for page N you write OFFSET (N-1)*5 LIMIT 5. OFFSET always follows LIMIT.
For example, for page 3 you write LIMIT 5 OFFSET 10: the first 10 rows are skipped and the next 5 are returned.
Practice OFFSET
From products ordered by price, skip 3 rows and return 2.
Starter code
-- skip and fetch
SELECT name, price
FROM products
ORDER BY priceSample solution
SELECT name, price
FROM products
ORDER BY price
LIMIT 2 OFFSET 3Expected output
name price Cetvel 3 Silgi 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Pagination
SELECT name FROM products
LIMIT 5 ____ 10;
SELECT name FROM products
LIMIT 5 OFFSET 10;
Explanation: OFFSET says how many rows to skip; together with LIMIT it builds pagination.
2 OFFSET: match the terms
| Term | Meaning |
|---|---|
OFFSET |
skip |
LIMIT |
limit |
ORDER BY |
sort |
DESC |
descending |
3 What does OFFSET do?
- Skips rows Correct answer
- Deletes rows
- Sorts them
- Selects columns
Explanation: OFFSET skips the first N rows.