0
0
MySQLquery~20 mins

Cursors for row iteration in MySQL - 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 iteration with FETCH
Consider the following MySQL stored procedure snippet that uses a cursor to iterate over a table employees with columns id and salary. What will be the value of total_salary after the cursor loop completes?

DECLARE done INT DEFAULT FALSE;
DECLARE emp_salary INT;
DECLARE total_salary INT DEFAULT 0;
DECLARE emp_cursor CURSOR FOR SELECT salary FROM employees WHERE id <= 3;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN emp_cursor;
read_loop: LOOP
  FETCH emp_cursor INTO emp_salary;
  IF done THEN
    LEAVE read_loop;
  END IF;
  SET total_salary = total_salary + emp_salary;
END LOOP;
CLOSE emp_cursor;

Assuming the employees table has rows with id 1, 2, 3 and salaries 1000, 1500, 2000 respectively.
ASyntax error due to missing cursor declaration
B4500
C1000
D0
Attempts:
2 left
💡 Hint
Think about how the cursor fetches each salary and adds it to the total.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in cursor declaration
Which of the following cursor declarations in MySQL is syntactically correct?
ADECLARE CURSOR emp_cursor FOR SELECT name FROM employees;
BCURSOR DECLARE emp_cursor FOR SELECT name FROM employees;
CDECLARE emp_cursor FOR CURSOR SELECT name FROM employees;
DDECLARE emp_cursor CURSOR FOR SELECT name FROM employees;
Attempts:
2 left
💡 Hint
Look at the order of keywords in the declaration.
🔧 Debug
advanced
2:00remaining
Why does this cursor loop never end?
Given the following MySQL cursor loop, why does it never exit?

DECLARE done INT DEFAULT FALSE;
DECLARE emp_name VARCHAR(50);
DECLARE emp_cursor CURSOR FOR SELECT name FROM employees;
-- Missing CONTINUE HANDLER

OPEN emp_cursor;
read_loop: LOOP
  FETCH emp_cursor INTO emp_name;
  IF done THEN
    LEAVE read_loop;
  END IF;
  -- process emp_name
END LOOP;
CLOSE emp_cursor;
ABecause the FETCH statement syntax is incorrect.
BBecause the cursor is not opened before the loop.
CBecause the CONTINUE HANDLER for NOT FOUND is missing, so 'done' is never set to TRUE.
DBecause the loop label is missing.
Attempts:
2 left
💡 Hint
Think about how the loop knows when to stop fetching rows.
optimization
advanced
2:00remaining
Optimizing cursor usage for large datasets
You have a cursor iterating over a large table to calculate the sum of a numeric column. Which approach is more efficient in MySQL?
AUse a single SQL query with SUM() aggregate function instead of a cursor loop.
BUse a cursor with a small FETCH size and commit after each fetch.
CUse a cursor and store each row in a temporary table before summing.
DUse nested cursors to process rows in batches.
Attempts:
2 left
💡 Hint
Think about what SQL is best at doing.
🧠 Conceptual
expert
2:30remaining
Understanding cursor scope and variable lifetime
In MySQL stored procedures, what happens to a cursor and its associated variables after the procedure ends?
AThe cursor and variables are automatically closed and deallocated when the procedure ends.
BThe cursor remains open and variables retain their values until the session ends.
CThe cursor is closed but variables keep their values after procedure ends.
DThe cursor and variables persist globally and can be accessed by other procedures.
Attempts:
2 left
💡 Hint
Consider the scope of variables and cursors inside stored procedures.