Recall & Review
beginner
What is a CURSOR in SQL?
A CURSOR is a database object used to retrieve, manipulate, and navigate through rows returned by a query one at a time.
Click to reveal answer
beginner
Why do we use a CURSOR instead of a simple SELECT statement?
We use a CURSOR when we need to process query results row by row, especially when each row requires individual handling or complex logic.
Click to reveal answer
intermediate
Name the main steps to use a CURSOR in SQL.
1. Declare the CURSOR with a SELECT query.<br>2. Open the CURSOR.<br>3. Fetch rows one by one.<br>4. Process each row.<br>5. Close the CURSOR.<br>6. Deallocate the CURSOR.
Click to reveal answer
intermediate
What happens if you forget to CLOSE or DEALLOCATE a CURSOR?
Not closing or deallocating a CURSOR can cause memory leaks and lock resources, slowing down or blocking the database.
Click to reveal answer
beginner
Give a simple example of DECLARE and OPEN a CURSOR in SQL.
DECLARE myCursor CURSOR FOR SELECT id, name FROM employees;<br>OPEN myCursor;
Click to reveal answer
What is the first step when using a CURSOR in SQL?
✗ Incorrect
You must first declare the CURSOR with a SELECT query before using it.
Which SQL command moves the CURSOR to the next row?
✗ Incorrect
FETCH retrieves the next row from the CURSOR.
What is the purpose of closing a CURSOR?
✗ Incorrect
Closing a CURSOR frees up resources and releases any locks held.
Which of these is NOT a typical CURSOR operation?
✗ Incorrect
INSERT is a data modification command, not a CURSOR operation.
When should you consider using a CURSOR?
✗ Incorrect
CURSORs are useful when processing rows individually.
Explain the main steps to use a CURSOR in SQL and why each step is important.
Think about how you read a book page by page and then close it when done.
You got /6 concepts.
Describe a real-life situation where using a CURSOR would be helpful in a database.
Imagine you want to greet each friend by name one at a time.
You got /4 concepts.