Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is keyset pagination?
Keyset pagination is a method to fetch data pages using a unique key from the last item of the previous page instead of using page numbers. It improves performance by avoiding slow database scans.
Click to reveal answer
beginner
Why is keyset pagination faster than offset pagination?
Keyset pagination uses a fixed key to jump directly to the next set of results, avoiding scanning all previous rows like offset pagination does. This reduces database load and speeds up queries.
Click to reveal answer
intermediate
In keyset pagination, what is typically used as the key?
A unique and indexed column, often a primary key or a timestamp, is used as the key to mark the last seen item and fetch the next page efficiently.
Click to reveal answer
intermediate
How does keyset pagination handle sorting?
Keyset pagination requires a consistent sort order on the key column(s) so that the next page starts exactly after the last item of the previous page.
Click to reveal answer
intermediate
What is a limitation of keyset pagination compared to offset pagination?
Keyset pagination cannot jump to arbitrary pages directly because it depends on the last seen key. It only supports moving forward or backward sequentially.
Click to reveal answer
What does keyset pagination use to fetch the next page?
AA random offset
BThe page number
CThe last item's unique key
DThe total number of items
✗ Incorrect
Keyset pagination uses the last item's unique key to fetch the next page efficiently.
Which of the following is a benefit of keyset pagination?
AAbility to jump to any page instantly
BWorks without any database indexes
CSimpler to implement than offset pagination
DFaster queries on large datasets
✗ Incorrect
Keyset pagination speeds up queries by avoiding scanning all previous rows.
What kind of column is best used as a key in keyset pagination?
AA non-unique text column
BA unique and indexed column
CA column with many NULL values
DA column with random values
✗ Incorrect
A unique and indexed column ensures efficient and correct pagination.
What is a common sorting requirement for keyset pagination?
ASort by the key column in a consistent order
BSort randomly
CNo sorting needed
DSort by page number
✗ Incorrect
Consistent sorting on the key column is needed to correctly fetch the next page.
Which is a limitation of keyset pagination?
ACannot jump directly to arbitrary pages
BSlower than offset pagination on small datasets
CRequires full table scans
DDoes not work with unique keys
✗ Incorrect
Keyset pagination only supports sequential navigation, not direct jumps to any page.
Explain how keyset pagination improves performance compared to offset pagination.
Think about how the database finds the next set of rows.
You got /4 concepts.
Describe a scenario where keyset pagination might not be the best choice.
Consider when you want to go directly to page 10 without going through pages 1 to 9.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using keyset pagination over traditional offset-based pagination in REST APIs?
easy
A. It requires less server memory by caching all pages in advance.
B. It allows jumping to any page number directly without sequential access.
C. It improves performance by avoiding slow offset queries on large datasets.
D. It automatically sorts data in descending order without extra code.
Solution
Step 1: Understand offset-based pagination issues
Offset pagination uses a page number and offset, which becomes slow on large datasets because the database must skip many rows.
Step 2: Recognize keyset pagination benefits
Keyset pagination uses a fixed key (like an ID) to fetch the next set of rows, avoiding the costly skip operation and improving performance.
Final Answer:
It improves performance by avoiding slow offset queries on large datasets. -> Option C
Quick Check:
Keyset pagination = better performance [OK]
Hint: Keyset uses keys, not offsets, for faster queries [OK]
Common Mistakes:
Thinking keyset allows random page jumps
Assuming keyset caches all data
Believing keyset changes sort order automatically
2. Which of the following is the correct SQL snippet to implement keyset pagination for a table users ordered by id?
easy
A. SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 20;
B. SELECT * FROM users WHERE id > 20 ORDER BY id LIMIT 10;
C. SELECT * FROM users WHERE id = 20 ORDER BY id LIMIT 10;
D. SELECT * FROM users ORDER BY id DESC LIMIT 10 OFFSET 20;
Solution
Step 1: Identify keyset pagination syntax
Keyset pagination uses a WHERE clause with a key (like id > last_seen_id) and a LIMIT to fetch the next page.
Step 2: Analyze each option
SELECT * FROM users WHERE id > 20 ORDER BY id LIMIT 10; uses WHERE id > 20 with ORDER BY id LIMIT 10, which matches keyset pagination logic.
Final Answer:
SELECT * FROM users WHERE id > 20 ORDER BY id LIMIT 10; -> Option B
Quick Check:
Keyset uses WHERE key > last_key [OK]
Hint: Keyset uses WHERE with key comparison, not OFFSET [OK]
Common Mistakes:
Using OFFSET instead of WHERE for pagination
Using equality (=) instead of greater than (>)
Not ordering results by the key column
3. Given this REST API endpoint using keyset pagination:
GET /items?last_id=50&limit=5
And the database table items with IDs: [45, 47, 50, 52, 55, 60, 65], what will be the IDs returned by this request?
medium
A. [52, 55, 60, 65]
B. [45, 47, 50, 52, 55]
C. [52, 55, 60, 65, 70]
D. [51, 52, 53, 54, 55]
Solution
Step 1: Understand keyset pagination with last_id=50
The API returns items with IDs greater than 50, limited to 5 results.
Step 2: Select IDs greater than 50 from the list
IDs greater than 50 are [52, 55, 60, 65]. There are only 4 such items, so all are returned.
Final Answer:
[52, 55, 60, 65] -> Option A
Quick Check:
IDs > 50 limited to 5 = [52, 55, 60, 65] [OK]
Hint: Return items with ID > last_id, up to limit [OK]
Common Mistakes:
Including items with ID equal to last_id
Using offset instead of key comparison
Assuming IDs are continuous numbers
4. You have this SQL query for keyset pagination:
SELECT * FROM orders WHERE order_date > '2024-01-01' ORDER BY order_date LIMIT 10;
But it returns duplicate rows when new orders are added. What is the likely cause?
medium
A. Ordering by a non-unique column causing duplicates
B. LIMIT value is too high causing repeats
C. Missing OFFSET clause to skip duplicates
D. Using > instead of >= in WHERE clause
Solution
Step 1: Identify ordering column uniqueness
Ordering by order_date alone can cause duplicates if multiple rows share the same date.
Step 2: Understand keyset pagination requirements
Keyset pagination requires ordering by a unique column or combination to avoid duplicates and missing rows.
Final Answer:
Ordering by a non-unique column causing duplicates -> Option A
Quick Check:
Order by unique key to avoid duplicates [OK]
Hint: Order by unique keys to prevent duplicates [OK]
Common Mistakes:
Thinking OFFSET fixes duplicates
Using >= instead of > causes duplicates
Assuming LIMIT controls duplicates
5. You want to implement keyset pagination on a REST API that returns products sorted by price ascending, then by id ascending to break ties. Which SQL WHERE clause correctly fetches the next page after last product with price=100 and id=50?
hard
A. WHERE price > 100 OR id > 50
B. WHERE price >= 100 AND id > 50
C. WHERE price > 100 AND id > 50
D. WHERE price > 100 OR (price = 100 AND id > 50)
Solution
Step 1: Understand multi-column keyset pagination
When ordering by multiple columns, the WHERE clause must handle the first column and then the second to break ties.
Step 2: Analyze the correct condition
The correct condition is price > 100 OR (price = 100 AND id > 50) to get all rows with higher price or same price but higher id.
Final Answer:
WHERE price > 100 OR (price = 100 AND id > 50) -> Option D
Quick Check:
Multi-column keyset uses OR + AND for tie-break [OK]
Hint: Use OR with AND to handle multi-column keys [OK]