Challenge - 5 Problems
FETCH FIRST Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Basic usage of FETCH FIRST
Given a table employees with columns
id and name, what will be the output of this query?SELECT id, name FROM employees ORDER BY id FETCH FIRST 3 ROWS ONLY;
PostgreSQL
SELECT id, name FROM employees ORDER BY id FETCH FIRST 3 ROWS ONLY;
Attempts:
2 left
💡 Hint
FETCH FIRST limits the number of rows returned after ordering.
✗ Incorrect
FETCH FIRST 3 ROWS ONLY returns exactly the first 3 rows after sorting by id ascending.
❓ query_result
intermediate2:00remaining
Using OFFSET with FETCH FIRST
What does this query return?
SELECT id FROM employees ORDER BY id OFFSET 5 ROWS FETCH FIRST 2 ROWS ONLY;
PostgreSQL
SELECT id FROM employees ORDER BY id OFFSET 5 ROWS FETCH FIRST 2 ROWS ONLY;
Attempts:
2 left
💡 Hint
OFFSET skips rows before FETCH FIRST limits the output.
✗ Incorrect
OFFSET 5 skips first 5 rows, FETCH FIRST 2 returns next 2 rows, so rows 6 and 7 by id ascending.
📝 Syntax
advanced2:00remaining
Correct syntax for FETCH FIRST with PERCENT
Which option shows the correct syntax to fetch the first 10 percent of rows from a table
orders ordered by order_date?Attempts:
2 left
💡 Hint
The keyword PERCENT comes immediately after the number.
✗ Incorrect
The correct syntax is FETCH FIRST PERCENT ROWS ONLY, so option A is valid.
🧠 Conceptual
advanced2:00remaining
Behavior of FETCH FIRST without ORDER BY
What is the behavior of the query below?
SELECT * FROM products FETCH FIRST 5 ROWS ONLY;
Attempts:
2 left
💡 Hint
Without ORDER BY, row order is unpredictable.
✗ Incorrect
FETCH FIRST limits rows but without ORDER BY, the rows returned can be any 5 rows.
❓ optimization
expert3:00remaining
Optimizing pagination with FETCH FIRST and OFFSET
Consider a large table
logs with millions of rows. Which query is generally more efficient for paginating to page 10 with 20 rows per page?A) SELECT * FROM logs ORDER BY timestamp OFFSET 180 ROWS FETCH FIRST 20 ROWS ONLY;Choose the correct statement.
B) SELECT * FROM logs WHERE timestamp > (SELECT timestamp FROM logs ORDER BY timestamp OFFSET 180 ROWS FETCH FIRST 1 ROW ONLY) ORDER BY timestamp FETCH FIRST 20 ROWS ONLY;
Attempts:
2 left
💡 Hint
OFFSET can cause performance issues on large tables.
✗ Incorrect
Query B uses a seek method by filtering on timestamp, avoiding scanning all rows before OFFSET, improving performance.