FETCH FIRST for SQL standard pagination in PostgreSQL - Time & Space Complexity
When we use FETCH FIRST in SQL, we want to get a limited number of rows from a query.
We ask: How does the time to get these rows grow as the table gets bigger?
Analyze the time complexity of the following SQL query using FETCH FIRST.
SELECT *
FROM employees
ORDER BY employee_id
FETCH FIRST 10 ROWS ONLY;
This query fetches the first 10 employees sorted by their ID.
Look for repeated work inside the query execution.
- Primary operation: Sorting the entire employees table by employee_id.
- How many times: The sorting happens once over all rows before fetching the first 10.
As the number of employees grows, sorting takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 operations |
| 100 | About 100 log 100 operations |
| 1000 | About 1000 log 1000 operations |
Pattern observation: The work grows a bit faster than the number of rows because sorting takes more effort as data grows.
Time Complexity: O(n log n)
This means the time to get the first 10 rows grows roughly like sorting all rows, which takes more time as the table gets bigger.
[X] Wrong: "FETCH FIRST 10 means the database only looks at 10 rows, so time stays the same no matter the table size."
[OK] Correct: The database usually sorts all rows first to know which 10 come first, so it processes the whole table before returning results.
Understanding how FETCH FIRST works helps you explain how databases handle limits and sorting efficiently, a useful skill for real projects and interviews.
What if we added an index on employee_id? How would the time complexity change?
Practice
FETCH FIRST 5 ROWS ONLY do in a query?Solution
Step 1: Understand FETCH FIRST clause
The clauseFETCH FIRST 5 ROWS ONLYlimits the number of rows returned by the query to 5.Step 2: Effect on query result
This means only the first 5 rows from the result set will be returned, no more.Final Answer:
Limits the query result to only the first 5 rows. -> Option AQuick Check:
FETCH FIRST limits rows = A [OK]
- Confusing FETCH FIRST with OFFSET
- Thinking FETCH FIRST sorts rows automatically
- Assuming FETCH FIRST removes rows instead of limiting
employees in PostgreSQL?Solution
Step 1: Recall correct FETCH FIRST syntax
The standard SQL syntax to limit rows isFETCH FIRST n ROWS ONLY. PostgreSQL supports this.Step 2: Check each option
SELECT * FROM employees FETCH FIRST 10 ROWS ONLY; uses correct syntax. Options A, B, and D are invalid in PostgreSQL.Final Answer:
SELECT * FROM employees FETCH FIRST 10 ROWS ONLY; -> Option BQuick Check:
Correct FETCH FIRST syntax = C [OK]
- Using LIMIT FIRST instead of FETCH FIRST
- Using TOP keyword which is SQL Server syntax
- Omitting ROWS ONLY after FETCH FIRST
products with columns id and price, what will this query return?SELECT id FROM products ORDER BY price DESC FETCH FIRST 3 ROWS ONLY;
Solution
Step 1: Analyze ORDER BY clause
The query orders products by price in descending order, so highest prices come first.Step 2: Apply FETCH FIRST 3 ROWS ONLY
This limits the output to only the first 3 rows after sorting, i.e., top 3 highest priced products.Final Answer:
The 3 products with the highest prices. -> Option CQuick Check:
ORDER BY DESC + FETCH FIRST = top rows [OK]
- Ignoring ORDER BY and thinking it returns random rows
- Confusing DESC with ASC order
- Assuming FETCH FIRST causes syntax error
SELECT name FROM customers FETCH FIRST 5 ROWS;
Solution
Step 1: Check FETCH FIRST syntax
The correct syntax requiresROWS ONLYafter the number, so it should beFETCH FIRST 5 ROWS ONLY.Step 2: Verify other parts
ORDER BY is recommended but not mandatory. OFFSET is not required. So the error is missing 'ONLY'.Final Answer:
Incorrect FETCH FIRST syntax; missing 'ONLY'. -> Option DQuick Check:
FETCH FIRST needs ROWS ONLY [OK]
- Omitting 'ONLY' after FETCH FIRST clause
- Thinking ORDER BY is mandatory with FETCH FIRST
- Confusing FETCH FIRST with OFFSET usage
created_at ascending. Which query correctly uses FETCH FIRST and OFFSET for this pagination?Solution
Step 1: Calculate OFFSET for page 3
Each page has 10 items, so page 3 starts at row 21. OFFSET is zero-based, so OFFSET = (3-1)*10 = 20.Step 2: Use FETCH FIRST to limit rows per page
FETCH FIRST 10 ROWS ONLY limits the result to 10 rows for the page.Step 3: Check query syntax order
OFFSET must come before FETCH FIRST. SELECT * FROM products ORDER BY created_at ASC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY; correctly orders OFFSET then FETCH FIRST.Final Answer:
SELECT * FROM products ORDER BY created_at ASC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY; -> Option AQuick Check:
OFFSET before FETCH FIRST for pagination [OK]
- Swapping OFFSET and FETCH FIRST order
- Using FETCH FIRST without OFFSET for pages >1
- Incorrect OFFSET calculation for page number
Ready to prove it?
Pass the test and this pattern moves unknown
