0
0
MySQLquery~20 mins

BEGIN, COMMIT, ROLLBACK in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after a COMMIT?

Consider the following sequence of SQL commands executed on a MySQL database with autocommit disabled:

BEGIN;
INSERT INTO accounts (id, balance) VALUES (1, 1000);
COMMIT;
SELECT balance FROM accounts WHERE id = 1;

What will the SELECT query return?

MySQL
BEGIN;
INSERT INTO accounts (id, balance) VALUES (1, 1000);
COMMIT;
SELECT balance FROM accounts WHERE id = 1;
ANULL
B1000
C0
DError: no such row
Attempts:
2 left
💡 Hint

Think about what COMMIT does to the changes made inside a transaction.

query_result
intermediate
2:00remaining
What happens after a ROLLBACK?

Given this sequence:

BEGIN;
INSERT INTO accounts (id, balance) VALUES (2, 500);
ROLLBACK;
SELECT balance FROM accounts WHERE id = 2;

What will the SELECT query return?

MySQL
BEGIN;
INSERT INTO accounts (id, balance) VALUES (2, 500);
ROLLBACK;
SELECT balance FROM accounts WHERE id = 2;
ANULL
B0
C500
DError: no such row
Attempts:
2 left
💡 Hint

ROLLBACK cancels all changes made in the current transaction.

🧠 Conceptual
advanced
2:00remaining
Which statement about transactions is TRUE?

Choose the correct statement about transactions in MySQL:

ACOMMIT makes all changes in the current transaction permanent.
BBEGIN starts a new transaction and locks the entire database.
CTransactions automatically commit after every SQL statement.
DROLLBACK permanently saves all changes made in the transaction.
Attempts:
2 left
💡 Hint

Recall the purpose of COMMIT and ROLLBACK in transactions.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this transaction block

Which option contains a syntax error in MySQL transaction commands?

Option A:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;

Option B:
START TRANSACTION;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
ROLLBACK;

Option C:
BEGIN TRANSACTION;
DELETE FROM accounts WHERE id = 3;
COMMIT;

Option D:
BEGIN;
INSERT INTO accounts (id, balance) VALUES (4, 200);
COMMIT;
Ao syntax error
BNo syntax error
Crorre xatnys oN
DSyntax error: 'BEGIN TRANSACTION' is invalid in MySQL
Attempts:
2 left
💡 Hint

Check the exact syntax MySQL accepts for starting transactions.

🔧 Debug
expert
3:00remaining
Why does this transaction not save changes?

A developer runs this code:

BEGIN;
UPDATE accounts SET balance = balance + 100 WHERE id = 1;
-- forgot to COMMIT
SELECT balance FROM accounts WHERE id = 1;

After disconnecting and reconnecting, the balance is unchanged. Why?

ABecause SELECT queries do not show uncommitted changes.
BBecause the UPDATE statement has a syntax error and did not run.
CBecause the transaction was never committed, so changes were lost on disconnect.
DBecause autocommit was enabled, so the transaction was committed automatically.
Attempts:
2 left
💡 Hint

Think about what happens to uncommitted transactions when the connection closes.