0
0
SQLquery~20 mins

CURSOR concept and usage in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cursor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of cursor fetching rows one by one
Consider a cursor declared on a table Employees with columns id and name. The cursor fetches rows ordered by id. What will be the output of the following pseudo-code?
DECLARE emp_cursor CURSOR FOR SELECT id, name FROM Employees ORDER BY id;
OPEN emp_cursor;
FETCH NEXT FROM emp_cursor INTO @id, @name;
PRINT @id, @name;
FETCH NEXT FROM emp_cursor INTO @id, @name;
PRINT @id, @name;
CLOSE emp_cursor;
DEALLOCATE emp_cursor;

Assume the table has rows: (2, 'Alice'), (1, 'Bob'), (3, 'Charlie').
SQL
DECLARE emp_cursor CURSOR FOR SELECT id, name FROM Employees ORDER BY id;
OPEN emp_cursor;
FETCH NEXT FROM emp_cursor INTO @id, @name;
PRINT @id, @name;
FETCH NEXT FROM emp_cursor INTO @id, @name;
PRINT @id, @name;
CLOSE emp_cursor;
DEALLOCATE emp_cursor;
A
1 Bob
2 Alice
B
2 Alice
1 Bob
C
1 Bob
3 Charlie
D
2 Alice
3 Charlie
Attempts:
2 left
💡 Hint
Remember the cursor fetches rows in the order specified by the ORDER BY clause.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in cursor declaration
Which of the following cursor declarations is syntactically correct in standard SQL?
ADECLARE my_cursor FOR CURSOR SELECT * FROM table_name;
BDECLARE my_cursor CURSOR FOR SELECT * FROM table_name;
CDECLARE CURSOR my_cursor FOR SELECT * FROM table_name;
DCURSOR DECLARE my_cursor FOR SELECT * FROM table_name;
Attempts:
2 left
💡 Hint
The correct order is DECLARE cursor_name CURSOR FOR query;
🧠 Conceptual
advanced
1:30remaining
Why use cursors instead of set-based queries?
Which of the following is the best reason to use a cursor in SQL?
ATo process rows one at a time when row-by-row operations are needed
BTo improve performance by avoiding loops
CTo replace all SELECT queries with faster alternatives
DTo automatically index tables for faster access
Attempts:
2 left
💡 Hint
Think about when you need to handle each row individually.
🔧 Debug
advanced
2:00remaining
Find the error in cursor usage
Given the following code snippet, what error will occur?
DECLARE emp_cursor CURSOR FOR SELECT id FROM Employees;
OPEN emp_cursor;
FETCH NEXT FROM emp_cursor INTO @emp_id;
CLOSE emp_cursor;
FETCH NEXT FROM emp_cursor INTO @emp_id;
SQL
DECLARE emp_cursor CURSOR FOR SELECT id FROM Employees;
OPEN emp_cursor;
FETCH NEXT FROM emp_cursor INTO @emp_id;
CLOSE emp_cursor;
FETCH NEXT FROM emp_cursor INTO @emp_id;
ANo error, fetch returns no rows
BError: Cursor not declared
CError: Cannot fetch from a closed cursor
DError: Variable @emp_id not declared
Attempts:
2 left
💡 Hint
Think about what happens if you fetch after closing the cursor.
optimization
expert
2:30remaining
Optimizing cursor usage for large datasets
You have a cursor processing 1 million rows. Which option best improves performance and resource usage?
AUse a LOCAL cursor to share across sessions
BUse a STATIC cursor to lock all rows
CUse a DYNAMIC cursor to see real-time changes
DUse a FAST_FORWARD cursor to reduce overhead
Attempts:
2 left
💡 Hint
Consider cursor types that minimize locking and resource use.