Challenge - 5 Problems
Cursor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of cursor fetching rows one by one
Consider a cursor declared on a table Employees with columns
Assume the table has rows: (2, 'Alice'), (1, 'Bob'), (3, 'Charlie').
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;
Attempts:
2 left
💡 Hint
Remember the cursor fetches rows in the order specified by the ORDER BY clause.
✗ Incorrect
The cursor selects rows ordered by id ascending: (1, Bob), (2, Alice), (3, Charlie). The first FETCH gets (1, Bob), the second FETCH gets (2, Alice).
📝 Syntax
intermediate1:30remaining
Identify the syntax error in cursor declaration
Which of the following cursor declarations is syntactically correct in standard SQL?
Attempts:
2 left
💡 Hint
The correct order is DECLARE cursor_name CURSOR FOR query;
✗ Incorrect
The correct syntax is: DECLARE cursor_name CURSOR FOR select_statement; Options A, B, and D have incorrect keyword order.
🧠 Conceptual
advanced1:30remaining
Why use cursors instead of set-based queries?
Which of the following is the best reason to use a cursor in SQL?
Attempts:
2 left
💡 Hint
Think about when you need to handle each row individually.
✗ Incorrect
Cursors allow row-by-row processing which is useful when operations depend on individual rows. They are not used to improve performance or replace set-based queries.
🔧 Debug
advanced2: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;
Attempts:
2 left
💡 Hint
Think about what happens if you fetch after closing the cursor.
✗ Incorrect
After closing a cursor, fetching from it causes an error because the cursor is no longer open.
❓ optimization
expert2:30remaining
Optimizing cursor usage for large datasets
You have a cursor processing 1 million rows. Which option best improves performance and resource usage?
Attempts:
2 left
💡 Hint
Consider cursor types that minimize locking and resource use.
✗ Incorrect
FAST_FORWARD cursors are read-only and forward-only, which reduces overhead and improves performance for large datasets. STATIC and DYNAMIC cursors have more overhead. LOCAL affects scope, not performance.