TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2819 889
๐Ÿš€ SQL Roadmap 2026 โ€” Part 19

SQL Transactions โ€” COMMIT, ROLLBACK & SAVEPOINT

SQL isn't only about retrieving data.

In real-world systems, SQL is also used to:

โ€ข Insert records

โ€ข Update records

โ€ข Delete records

โ€ข Process financial transactions

โ€ข Move money between accounts

โ€ข Update multiple related tables

โ€ข Maintain data consistency

But what happens when a process involves multiple SQL statements and one of them fails?

That's where SQL transactions become important.

1๏ธโƒฃ What Is a Transaction?

A transaction is a group of one or more SQL operations that are treated as a logical unit of work.

For example, transferring money between two accounts may involve:

โ€ข Account A โ†’ Deduct โ‚น1,000

โ€ข Account B โ†’ Add โ‚น1,000

These two operations should normally be treated as one transaction.

You don't want this situation:

โ€ข Account A โ†’ โ‚น1,000 deducted โœ…

โ€ข Account B โ†’ โ‚น1,000 not credited โŒ

The transaction mechanism helps maintain consistency.

2๏ธโƒฃ Basic Transaction Flow

BEGIN TRANSACTION

โ†“

SQL Statement 1

โ†“

SQL Statement 2

โ†“

SQL Statement 3

โ†“

COMMIT

If something goes wrong:

BEGIN TRANSACTION

โ†“

SQL Statement 1

โ†“

SQL Statement 2 โŒ

โ†“

ROLLBACK

โ†“

Changes undone

3๏ธโƒฃ BEGIN TRANSACTION

Depending on the database, you may see:

BEGIN TRANSACTION;
-- or
BEGIN;


Some database systems handle transaction boundaries differently, so exact syntax varies.

For example:

BEGIN;

UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 101;


The transaction has started.

4๏ธโƒฃ COMMIT

"COMMIT" permanently saves the changes made during the transaction.

Example:

BEGIN;

UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 101;

UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 202;

COMMIT;


After the transaction is successfully committed, the changes become durable according to the database's transaction rules.

5๏ธโƒฃ ROLLBACK

"ROLLBACK" reverses uncommitted changes within the transaction.

Example:

BEGIN;

UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 101;

UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 202;

ROLLBACK;


The changes made in that transaction are undone.

6๏ธโƒฃ COMMIT vs ROLLBACK

This is a common interview question.

โ€ข COMMIT โ†’ Save the transaction

โ€ข ROLLBACK โ†’ Undo uncommitted changes

Example:

BEGIN;
UPDATE customers
SET status = 'Inactive'
WHERE customer_id = 101;
COMMIT;


The change is committed.

Whereas:

BEGIN;
UPDATE customers
SET status = 'Inactive'
WHERE customer_id = 101;
ROLLBACK;


The uncommitted update is rolled back.

7๏ธโƒฃ SAVEPOINT

Sometimes you don't want to roll back the entire transaction.

You can create a "SAVEPOINT".

Example:

BEGIN;

UPDATE customers
SET status = 'Active'
WHERE customer_id = 101;

SAVEPOINT customer_update;

UPDATE customers
SET status = 'Inactive'
WHERE customer_id = 102;


Now suppose you don't want the second update.

You can roll back to the savepoint:

ROLLBACK TO SAVEPOINT customer_update;
  • โค 3
More from @sqlanalyst
  1. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  2. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  3. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
  4. Sep 28, 2026๐Ÿง  Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from thโ€ฆ
  5. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
  6. Sep 27, 2026Data Analytics Roadmap | |-- Fundamentals | |-- Mathematics | | |-- Descriptive Statisticsโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’