Challenge - 5 Problems
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
π§ Conceptual
intermediate2:00remaining
Why do transactions help keep data accurate?
Imagine you are transferring money between two bank accounts. Why does using a transaction help keep the data correct?
Attempts:
2 left
π‘ Hint
Think about what happens if only part of the transfer happens.
β Incorrect
Transactions group multiple steps so they all succeed or all fail. This prevents partial updates that could cause errors like money disappearing or being counted twice.
β query_result
intermediate2:00remaining
What is the output after a failed transaction?
Given this SQL code, what will be the final balance in the accounts table?
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
ROLLBACK;
SELECT id, balance FROM accounts WHERE id IN (1, 2);
MySQL
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; ROLLBACK; SELECT id, balance FROM accounts WHERE id IN (1, 2);
Attempts:
2 left
π‘ Hint
ROLLBACK undoes all changes made in the transaction.
β Incorrect
ROLLBACK cancels all changes made since BEGIN, so balances remain as before the transaction started.
π Syntax
advanced2:00remaining
Identify the correct transaction syntax
Which SQL snippet correctly starts a transaction, updates a table, and commits the changes?
Attempts:
2 left
π‘ Hint
Look for the standard MySQL transaction commands.
β Incorrect
In MySQL, START TRANSACTION begins a transaction, and COMMIT saves changes. Other options use incorrect or incomplete syntax.
β optimization
advanced2:00remaining
How to improve transaction performance without losing integrity?
You have a transaction that updates many rows but runs slowly. Which approach improves speed while keeping data safe?
Attempts:
2 left
π‘ Hint
Think about balancing transaction size and locking time.
β Incorrect
Smaller transactions reduce lock time and resource use, improving speed while preserving atomicity and consistency.
π§ Debug
expert3:00remaining
Why does this transaction cause a deadlock?
Two transactions run:
Transaction 1:
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;
Transaction 2:
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE id = 2;
UPDATE accounts SET balance = balance + 30 WHERE id = 1;
COMMIT;
Why might these cause a deadlock?
Attempts:
2 left
π‘ Hint
Think about how locks are acquired and the order of operations.
β Incorrect
Deadlocks happen when two transactions wait for each otherβs locks. Here, each locks one row and waits for the otherβs locked row, causing a cycle.