LIMIT and OFFSET pagination in PostgreSQL - Time & Space Complexity
When using LIMIT and OFFSET to paginate query results, it's important to understand how the time to get results changes as the page number grows.
We want to know how the query's work increases when we ask for later pages.
Analyze the time complexity of the following query for pagination.
SELECT * FROM products
ORDER BY product_id
LIMIT 10 OFFSET 1000;
This query fetches 10 products but skips the first 1000 rows, simulating page 101 of results.
Look for repeated work done by the database engine.
- Primary operation: Scanning and skipping rows up to the OFFSET value.
- How many times: The database reads through 1000 rows before returning 10 rows.
As OFFSET grows, the database must skip more rows before returning results.
| Input Size (OFFSET n) | Approx. Operations |
|---|---|
| 10 | Reads about 10 rows |
| 100 | Reads about 100 rows |
| 1000 | Reads about 1000 rows |
Pattern observation: The work grows roughly in direct proportion to the OFFSET number.
Time Complexity: O(n)
This means the time to get results grows linearly as the OFFSET number increases.
[X] Wrong: "OFFSET just jumps directly to the page, so time stays the same no matter the page."
[OK] Correct: The database still has to scan and skip all rows before the OFFSET, so more OFFSET means more work.
Understanding how LIMIT and OFFSET affect query time helps you explain performance trade-offs clearly and shows you know how databases handle pagination internally.
"What if we replaced OFFSET with a WHERE clause filtering by an indexed column? How would the time complexity change?"
Practice
LIMIT clause do in a PostgreSQL query?Solution
Step 1: Understand the role of LIMIT
The LIMIT clause restricts the number of rows returned by a query to a specified count.Step 2: Differentiate from OFFSET
OFFSET skips rows, but LIMIT controls the count of rows returned after skipping.Final Answer:
It controls how many rows the query returns. -> Option BQuick Check:
LIMIT = row count control [OK]
- Confusing LIMIT with OFFSET
- Thinking LIMIT sorts rows
- Assuming LIMIT groups rows
Solution
Step 1: Understand OFFSET and LIMIT order
OFFSET 5 skips first 5 rows, LIMIT 10 returns next 10 rows.Step 2: Check syntax correctness
PostgreSQL allows LIMIT then OFFSET;LIMIT 10 OFFSET 5is correct.Final Answer:
SELECT * FROM table LIMIT 10 OFFSET 5; -> Option AQuick Check:
LIMIT 10 OFFSET 5 = 10 rows after skipping 5 [OK]
- Swapping LIMIT and OFFSET values
- Using OFFSET larger than total rows
- Incorrect order of LIMIT and OFFSET
employees with 15 rows ordered by id, what rows will this query return?SELECT * FROM employees ORDER BY id LIMIT 5 OFFSET 10;
Solution
Step 1: Understand OFFSET 10
OFFSET 10 skips the first 10 rows (id 1 to 10).Step 2: LIMIT 5 after skipping
LIMIT 5 returns the next 5 rows, which are id 11 to 15.Final Answer:
Rows with id 11 to 15 -> Option AQuick Check:
OFFSET 10 + LIMIT 5 = rows 11-15 [OK]
- Counting OFFSET as inclusive
- Mixing row numbers with id values
- Assuming OFFSET starts at 1
SELECT * FROM products LIMIT OFFSET 5 10;
Solution
Step 1: Analyze LIMIT and OFFSET syntax
LIMIT must be followed by a number, then OFFSET with its number.Step 2: Check given query
Query has 'LIMIT OFFSET 5 10' which is invalid syntax; LIMIT lacks a number before OFFSET.Final Answer:
Syntax error: LIMIT must have a number before OFFSET. -> Option CQuick Check:
LIMIT needs number before OFFSET [OK]
- Writing OFFSET before LIMIT number
- Omitting numbers after LIMIT or OFFSET
- Assuming order doesn't matter
Solution
Step 1: Calculate OFFSET for page 3
Page 3 means skipping first 2 pages: 2 * 20 = 40 rows to skip.Step 2: Set LIMIT to page size
LIMIT is number of rows per page, so LIMIT 20.Step 3: Form the query
Use LIMIT 20 OFFSET 40 to get page 3 products.Final Answer:
SELECT * FROM products LIMIT 20 OFFSET 40; -> Option DQuick Check:
Page 3 offset = 40, limit = 20 [OK]
- Using OFFSET as page number
- Swapping LIMIT and OFFSET values
- Calculating OFFSET incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
