Ad space (banner)
🗄️SQL Lessons
Lesson 12 / 50

Understanding Transactions

This lesson introduces transactions, so you can safely group multiple SQL statements together. It's for anyone searching "SQL transaction tutorial" or "what is a database transaction."

A transaction groups several SQL statements so they either all succeed or all fail together, started with BEGIN TRANSACTION and finalized with COMMIT. It's built for operations — like a bank transfer — where a partial failure would be a disaster.

The example wraps two UPDATE statements — subtracting 300 from Alice's balance and adding 300 to Bob's — inside BEGIN TRANSACTION and COMMIT. The key idea: those two updates are treated as one indivisible "transfer."

A common early mistake is forgetting that changes aren't final until COMMIT runs — once a transaction starts, it must be closed with either COMMIT (finalize) or ROLLBACK (undo). If only one side of a transfer succeeded, money would vanish or duplicate — a serious inconsistency. Transactions exist specifically to prevent this kind of "half-finished" state, making them one of the most important safety mechanisms in a database.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)