0
0
PostgreSQLquery~20 mins

Performing operations on cursors in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cursor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of FETCH with SCROLL cursor
Consider the following PostgreSQL commands executed in order:

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;
A3
B1
C5
DNULL
Attempts:
2 left
💡 Hint
The ABSOLUTE option fetches the row at the exact position in the result set.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in cursor declaration
Which of the following cursor declarations is syntactically correct in PostgreSQL?
ADECLARE cur1 CURSOR IN SELECT * FROM users;
BDECLARE cur1 CURSOR SELECT * FROM users;
CDECLARE cur1 CURSOR FOR (SELECT * FROM users);
DDECLARE cur1 CURSOR FOR SELECT * FROM users;
Attempts:
2 left
💡 Hint
Cursor declaration requires the keyword FOR before the query.
optimization
advanced
2: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?
ADECLARE cur NO SCROLL CURSOR WITH HOLD FOR SELECT * FROM large_table;
BDECLARE cur NO SCROLL CURSOR FOR SELECT * FROM large_table;
CDECLARE cur CURSOR FOR SELECT * FROM large_table;
DDECLARE cur SCROLL CURSOR WITH HOLD FOR SELECT * FROM large_table;
Attempts:
2 left
💡 Hint
WITH HOLD allows cursor to remain open after commit, NO SCROLL reduces overhead.
🔧 Debug
advanced
2:00remaining
Debugging cursor fetch error
Given the following commands:

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?
AERROR: cursor is not scrollable
BERROR: no prior row found
CERROR: cursor does not exist
DNo error, returns previous row
Attempts:
2 left
💡 Hint
By default, cursors are not scrollable unless declared SCROLL.
🧠 Conceptual
expert
3: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?
AThe cursor resets to the first row but remains open
BThe cursor remains open and usable in subsequent transactions
CThe cursor is automatically closed and cannot be used further
DThe cursor switches to read-only mode
Attempts:
2 left
💡 Hint
WITH HOLD controls cursor persistence after commit.