0
0
PostgreSQLquery~20 mins

Cursor declaration and usage 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 declared cursor
Given the following cursor declaration and fetch commands, what will be the output of the FETCH command?
PostgreSQL
DECLARE emp_cursor CURSOR FOR SELECT id, name FROM employees ORDER BY id;
FETCH NEXT FROM emp_cursor;
AReturns no rows because cursor is not opened
BReturns all rows from employees ordered by id
CSyntax error due to missing OPEN statement
DReturns the first row with columns id and name from employees ordered by id
Attempts:
2 left
💡 Hint
Remember that DECLARE only defines the cursor, FETCH retrieves rows.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in cursor usage
Which option contains a syntax error when declaring and using a cursor in PostgreSQL?
A
DECLARE my_cursor CURSOR FOR SELECT * FROM products;
OPEN my_cursor;
FETCH NEXT my_cursor;
CLOSE my_cursor;
B
DECLARE my_cursor CURSOR FOR SELECT * FROM products;
OPEN my_cursor;
FETCH NEXT FROM my_cursor;
CLOSE my_cursor;
C
DECLARE my_cursor CURSOR FOR SELECT * FROM products;
FETCH NEXT FROM my_cursor;
CLOSE my_cursor;
D
OPEN my_cursor;
DECLARE my_cursor CURSOR FOR SELECT * FROM products;
FETCH NEXT FROM my_cursor;
CLOSE my_cursor;
Attempts:
2 left
💡 Hint
Cursor must be declared before it is opened.
optimization
advanced
2:00remaining
Optimizing cursor usage for large data sets
Which option best optimizes cursor usage to reduce memory consumption when processing a large table in PostgreSQL?
ADeclare the cursor as SCROLL to allow backward and forward movement
BDeclare the cursor WITH HOLD to keep it open after commit
CDeclare the cursor WITHOUT HOLD and fetch rows in small batches
DDeclare the cursor as INSENSITIVE to avoid changes during fetch
Attempts:
2 left
💡 Hint
Fetching in small batches helps manage memory better.
🧠 Conceptual
advanced
2:00remaining
Understanding cursor behavior after transaction commit
What happens to a cursor declared WITHOUT HOLD after the transaction that opened it commits in PostgreSQL?
AThe cursor is automatically closed and cannot be fetched from after commit
BThe cursor remains open and can be fetched from after commit
CThe cursor changes to a HOLD cursor automatically
DThe cursor's position resets to the first row after commit
Attempts:
2 left
💡 Hint
Think about cursor lifespan related to transactions.
🔧 Debug
expert
2:00remaining
Debugging cursor fetch error
Given this code snippet, what error will occur when running the FETCH statement? BEGIN; DECLARE cur CURSOR FOR SELECT id FROM users WHERE active = true; OPEN cur; CLOSE cur; FETCH NEXT FROM cur; COMMIT;
PostgreSQL
BEGIN;
DECLARE cur CURSOR FOR SELECT id FROM users WHERE active = true;
OPEN cur;
CLOSE cur;
FETCH NEXT FROM cur;
COMMIT;
AFETCH will return no rows but no error
BFETCH will raise a 'cursor is not open' error
CFETCH will return the first active user id
DFETCH will raise a syntax error
Attempts:
2 left
💡 Hint
Consider what happens after closing a cursor.