Challenge - 5 Problems
Cursor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of FETCH with SCROLL cursor
Consider the following PostgreSQL commands executed in order:
What is the output of the FETCH command?
BEGIN;
DECLARE mycursor SCROLL CURSOR FOR SELECT generate_series(1,5);
FETCH ABSOLUTE 3 FROM mycursor;
What is the output of the FETCH command?
PostgreSQL
BEGIN; DECLARE mycursor SCROLL CURSOR FOR SELECT generate_series(1,5); FETCH ABSOLUTE 3 FROM mycursor;
Attempts:
2 left
💡 Hint
The ABSOLUTE option fetches the row at the exact position in the result set.
✗ Incorrect
The cursor selects numbers 1 to 5. FETCH ABSOLUTE 3 returns the third row, which is 3.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in cursor declaration
Which of the following cursor declarations is syntactically correct in PostgreSQL?
Attempts:
2 left
💡 Hint
Cursor declaration requires the keyword FOR before the query.
✗ Incorrect
Option D correctly uses DECLARE cur1 CURSOR FOR followed by the SELECT statement. Other options miss FOR or use invalid syntax.
❓ optimization
advanced2:30remaining
Optimizing cursor usage for large datasets
You need to process a large table row by row using a cursor in PostgreSQL. Which option optimizes memory usage and avoids locking the entire table during processing?
Attempts:
2 left
💡 Hint
WITH HOLD allows cursor to remain open after commit, NO SCROLL reduces overhead.
✗ Incorrect
Option B uses NO SCROLL to reduce overhead and avoids locking by not using WITH HOLD, which keeps the cursor tied to the transaction. WITH HOLD cursors remain open after commit but can hold locks longer.
🔧 Debug
advanced2:00remaining
Debugging cursor fetch error
Given the following commands:
What error will the last FETCH cause?
BEGIN;
DECLARE mycur CURSOR FOR SELECT id FROM products WHERE price > 100;
FETCH NEXT FROM mycur;
FETCH PRIOR FROM mycur;
What error will the last FETCH cause?
Attempts:
2 left
💡 Hint
By default, cursors are not scrollable unless declared SCROLL.
✗ Incorrect
The cursor was declared without SCROLL, so FETCH PRIOR causes an error because the cursor cannot move backward.
🧠 Conceptual
expert3:00remaining
Understanding cursor behavior with transaction commits
In PostgreSQL, what happens to a cursor declared WITHOUT the WITH HOLD option when the transaction that declared it commits?
Attempts:
2 left
💡 Hint
WITH HOLD controls cursor persistence after commit.
✗ Incorrect
Cursors declared without WITH HOLD close automatically at transaction commit and cannot be used afterward.