LIMIT/OFFSET — Complete Guide
LIMIT/OFFSET — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 16 of 100
LIMIT/OFFSET
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — SQL & Queries
What is this?
LIMIT caps how many rows return; OFFSET skips that many rows from the start of the sorted result. Together they build pagination — page 2 is LIMIT 10 OFFSET 10.
Why should you care?
Flipkart mobile lists 20 products per scroll; OFFSET grows with page number so users do not download the entire catalog at once.
See it live — copy this example
Run in pgAdmin or psql.
SELECT order_id, created_at, total_amount
FROM orders
ORDER BY created_at DESC
LIMIT 15 OFFSET 30;
What happened?
- Orders sort newest first.
- OFFSET 30 skips the first 30 rows (pages 1–2 if page size is 15).
- LIMIT 15 returns page 3.
- Large OFFSET on huge tables gets slow — keyset pagination is the production upgrade.
Practice next
- Insert at least 50 orders with varied created_at.
- Run LIMIT 15 OFFSET 0 for page 1.
- Run OFFSET 30 for page 3 and compare order_ids.
- Fetch page 2 with LIMIT 10 OFFSET 10 on products sorted by price.
- Replace OFFSET with WHERE order_id < $cursor ORDER BY order_id DESC LIMIT 15.
Remember
LIMIT = page size; OFFSET = (page-1)*size. Always pair pagination with stable ORDER BY. Keyset pagination beats large OFFSET at scale.
PostgresVerse order history
Customer app loads order history three pages deep without scanning entire ledger.
Outcome: API returns 15 rows per request; bandwidth stays low on 4G.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!