0
0
PostgreSQLquery~20 mins

FETCH FIRST for SQL standard pagination in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FETCH FIRST Pagination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
AReturns the last 3 rows ordered by id ascending
BReturns all rows except the first 3
CReturns the first 3 rows ordered by id ascending
DSyntax error due to FETCH FIRST usage
Attempts:
2 left
💡 Hint
FETCH FIRST limits the number of rows returned after ordering.
query_result
intermediate
2: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;
ARows 6 and 7 ordered by id ascending
BRows 1 and 2 ordered by id ascending
CRows 5 and 6 ordered by id ascending
DSyntax error due to OFFSET and FETCH FIRST combination
Attempts:
2 left
💡 Hint
OFFSET skips rows before FETCH FIRST limits the output.
📝 Syntax
advanced
2: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?
ASELECT * FROM orders ORDER BY order_date FETCH FIRST 10 PERCENT ROWS ONLY;
BSELECT * FROM orders ORDER BY order_date FETCH FIRST 10 ROWS PERCENT ONLY;
CSELECT * FROM orders ORDER BY order_date FETCH FIRST PERCENT 10 ROWS ONLY;
DSELECT * FROM orders ORDER BY order_date FETCH FIRST ROWS 10 PERCENT ONLY;
Attempts:
2 left
💡 Hint
The keyword PERCENT comes immediately after the number.
🧠 Conceptual
advanced
2:00remaining
Behavior of FETCH FIRST without ORDER BY
What is the behavior of the query below?
SELECT * FROM products FETCH FIRST 5 ROWS ONLY;
AReturns the last 5 rows from products
BReturns the first 5 rows sorted by primary key automatically
CSyntax error because ORDER BY is required with FETCH FIRST
DReturns any 5 rows from products, order is not guaranteed
Attempts:
2 left
💡 Hint
Without ORDER BY, row order is unpredictable.
optimization
expert
3: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;
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;
Choose the correct statement.
AQuery A is more efficient because OFFSET is simple to use
BQuery B is more efficient because it avoids scanning all rows before OFFSET
CBoth queries have the same performance
DNeither query works because FETCH FIRST cannot be combined with OFFSET
Attempts:
2 left
💡 Hint
OFFSET can cause performance issues on large tables.