LIMIT and OFFSET for pagination in MySQL - Time & Space Complexity
When using LIMIT and OFFSET in MySQL, we want to know how the time to get results changes as we ask for different pages of data.
We ask: How does the work grow when we move to later pages?
Analyze the time complexity of the following code snippet.
SELECT * FROM products
ORDER BY product_id
LIMIT 10 OFFSET 30;
This query gets 10 products starting from the 31st product, ordered by product_id.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows to skip OFFSET and then fetching LIMIT rows.
- How many times: The database reads rows up to OFFSET + LIMIT to return the page.
As OFFSET grows, the database must skip more rows before returning the page.
| Input Size (OFFSET) | Approx. Rows Read |
|---|---|
| 10 | About 20 rows (10 skipped + 10 returned) |
| 100 | About 110 rows (100 skipped + 10 returned) |
| 1000 | About 1010 rows (1000 skipped + 10 returned) |
Pattern observation: The work grows roughly in a straight line with OFFSET size.
Time Complexity: O(n)
This means the time to get a page grows linearly with how far into the list you go.
[X] Wrong: "Using OFFSET doesn't affect performance because we only get a small page of results."
[OK] Correct: The database still reads and skips all rows before the OFFSET, so bigger OFFSET means more work.
Understanding how LIMIT and OFFSET affect query time helps you explain pagination performance clearly and shows you know how databases handle data retrieval.
"What if we replaced OFFSET with a WHERE condition to start after a known product_id? How would the time complexity change?"
Practice
LIMIT clause do in a MySQL query?Solution
Step 1: Understand the purpose of LIMIT
The LIMIT clause restricts the number of rows returned by a query.Step 2: Compare with other options
Ordering is done by ORDER BY, filtering by WHERE, and schema changes by DDL commands, not LIMIT.Final Answer:
It controls how many rows the query returns. -> Option AQuick Check:
LIMIT = number of rows [OK]
- Confusing LIMIT with ORDER BY
- Thinking LIMIT filters rows by condition
- Assuming LIMIT changes table structure
Solution
Step 1: Understand LIMIT and OFFSET syntax
MySQL supports bothLIMIT count OFFSET offsetandLIMIT offset, countsyntax.Step 2: Identify correct syntax for 10 rows starting at 7th row
OFFSET 6 means skip first 6 rows, then LIMIT 10 means get next 10 rows. SELECT * FROM table LIMIT 10 OFFSET 6; uses this correctly.Final Answer:
SELECT * FROM table LIMIT 10 OFFSET 6; -> Option BQuick Check:
LIMIT count OFFSET offset = correct syntax [OK]
- Swapping LIMIT and OFFSET values
- Using OFFSET before LIMIT incorrectly
- Confusing LIMIT offset,count with LIMIT count OFFSET offset
users with rows numbered by id from 1 to 20, what rows will this query return?SELECT * FROM users ORDER BY id ASC LIMIT 5 OFFSET 10;Solution
Step 1: Understand OFFSET and LIMIT effect on rows
OFFSET 10 skips first 10 rows (id 1 to 10), LIMIT 5 returns next 5 rows.Step 2: Identify which rows are returned
After skipping 10 rows, rows with id 11 to 15 are returned.Final Answer:
Rows with id 11 to 15 -> Option CQuick Check:
OFFSET 10 + LIMIT 5 = rows 11-15 [OK]
- Counting OFFSET as inclusive row
- Mixing up OFFSET and LIMIT order
- Assuming OFFSET starts at 1 instead of 0
SELECT * FROM products LIMIT OFFSET 3, 5;Solution
Step 1: Check correct syntax for LIMIT and OFFSET
Correct syntax is eitherLIMIT count OFFSET offsetorLIMIT offset, countwithout the word OFFSET in the second form.Step 2: Identify error in given query
The query uses both LIMIT and OFFSET keywords together incorrectly with a comma, causing syntax error.Final Answer:
Syntax error: LIMIT should come before OFFSET without comma. -> Option AQuick Check:
LIMIT offset,count or LIMIT count OFFSET offset [OK]
- Mixing LIMIT and OFFSET keywords with commas
- Using OFFSET before LIMIT
- Adding OFFSET keyword with comma syntax
created_at descending. Which query correctly uses LIMIT and OFFSET?Solution
Step 1: Calculate OFFSET for page 4 with 15 items per page
Page 1 shows rows 0-14, page 2 rows 15-29, page 3 rows 30-44, so page 4 starts at offset 45.Step 2: Write query with LIMIT and OFFSET
LIMIT 15 means 15 rows per page, OFFSET 45 skips first 45 rows. SELECT * FROM products ORDER BY created_at DESC LIMIT 15 OFFSET 45; matches this.Final Answer:
SELECT * FROM products ORDER BY created_at DESC LIMIT 15 OFFSET 45; -> Option DQuick Check:
OFFSET = (page-1)*page_size = 45 [OK]
- Swapping LIMIT and OFFSET values
- Calculating OFFSET incorrectly
- Using LIMIT as total rows instead of per page
Ready to prove it?
Pass the test and this pattern moves unknown
