Challenge - 5 Problems
Savepoint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of nested savepoints in a transaction
Consider the following SQL transaction with savepoints:
What is the final balance of the account with id = 1 after this transaction?
BEGIN TRANSACTION;
INSERT INTO accounts (id, balance) VALUES (1, 100);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
SAVEPOINT sp2;
UPDATE accounts SET balance = balance + 30 WHERE id = 1;
ROLLBACK TO SAVEPOINT sp1;
COMMIT;
What is the final balance of the account with id = 1 after this transaction?
Attempts:
2 left
💡 Hint
Rolling back to savepoint sp1 undoes changes after sp1 but keeps changes before it.
✗ Incorrect
The initial insert sets balance to 100. The update subtracting 50 happens after sp1, then sp2 is set and another update adds 30. Rolling back to sp1 undoes both updates after sp1, so balance remains 100.
🧠 Conceptual
intermediate1:30remaining
Purpose of savepoints in transactions
What is the main purpose of using savepoints within a database transaction?
Attempts:
2 left
💡 Hint
Think about what happens when you want to undo some changes but keep others in the same transaction.
✗ Incorrect
Savepoints let you mark points inside a transaction so you can roll back to them if needed without aborting the whole transaction.
📝 Syntax
advanced1:30remaining
Identify the syntax error in savepoint usage
Which of the following SQL statements will cause a syntax error when using savepoints inside a transaction?
Attempts:
2 left
💡 Hint
Check if the savepoint statement includes all required parts.
✗ Incorrect
The statement 'SAVEPOINT;' is missing the savepoint name, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this rollback not undo changes?
Given this transaction:
Why does the rollback not undo the update?
BEGIN TRANSACTION;
INSERT INTO orders (id, amount) VALUES (1, 200);
SAVEPOINT sp1;
UPDATE orders SET amount = 300 WHERE id = 1;
ROLLBACK TO SAVEPOINT sp2;
COMMIT;
Why does the rollback not undo the update?
Attempts:
2 left
💡 Hint
Check if the savepoint name used in rollback exists.
✗ Incorrect
The rollback references sp2 which was never created, so it does nothing and the update remains.
❓ optimization
expert2:30remaining
Optimizing transaction with multiple savepoints
You have a long transaction with multiple savepoints to handle errors in different steps. Which approach optimizes performance and resource usage best?
Attempts:
2 left
💡 Hint
Think about balancing rollback flexibility with overhead of managing savepoints.
✗ Incorrect
Creating savepoints only where needed and releasing them when done reduces overhead and improves performance.