TalentJR

🗄️ SQL for beginners · Class 12 · Lesson 12 of 18 · Free

SQL Transactions – BEGIN, COMMIT and ROLLBACK for All-or-Nothing Changes

⏱ About 35 minutes · ▶ run real SQL here — nothing to install · no sign-in

In this lesson you will

  • Group several changes into one transaction with BEGIN and COMMIT
  • Undo unfinished changes with ROLLBACK
  • Explain what ACID means using a fee payment

A parent pays the July school fee of ₹4,500 from the school wallet. Three things must happen: take the money, mark the fee paid, and print a receipt. If the power goes off after step one, the money is gone but the fee still shows unpaid. A transaction makes the three steps happen together, or not at all.

What is a transaction in SQL?

A transaction is a group of SQL statements that the database treats as one job. BEGIN starts the group. COMMIT saves all of it for good.

The setup adds two tables to the school database: wallets (money parents keep with the school) and receipts. Below, Aman's July fee is paid in three steps inside one transaction.

Until COMMIT runs, the changes are not final. Try it: change student 2 to student 6 and month '2026-07' to '2026-06' everywhere, then run again to pay Kabir's fee.

📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
🗄️ SQL — you can edit this query
Result
Transaction started.

1 row updated.

1 row updated.

1 row inserted.

Transaction committed (saved).

┌─────────┬──────┬────────────┐
│ balance │ paid │ receipt_no │
├─────────┼──────┼────────────┤
│ 1500    │ 1    │ 1          │
└─────────┴──────┴────────────┘
(1 row)

How does ROLLBACK undo changes in SQL?

ROLLBACK throws away every change since BEGIN. The data goes back to how it was before the transaction started.

Below, someone deletes all fee records by mistake. Inside the transaction, the count really is 0. After ROLLBACK, all 11 rows are back.

Try it: change ROLLBACK to COMMIT and run again. Now the rows are gone for good.

📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
🗄️ SQL — you can edit this query
Result
Transaction started.

11 rows deleted.

┌──────────┐
│ fee_rows │
├──────────┤
│ 0        │
└──────────┘
(1 row)

Transaction rolled back (undone).

┌──────────┐
│ fee_rows │
├──────────┤
│ 11       │
└──────────┘
(1 row)

What happens if one step fails halfway?

Dev Patil owes ₹4,000 but his wallet has only ₹3,000. The app marks the fee paid first, then tries to take the money. The safe UPDATE only takes money WHERE balance >= 4000, so it changes 0 rows.

The app sees "0 rows updated" and calls ROLLBACK. That also undoes the first step, so the fee is still unpaid. Nothing is half-done.

Without BEGIN, step one would already be saved: Dev's fee would show paid with no money taken. Try it: change ROLLBACK to COMMIT and run again. You will see exactly that bug.

📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
🗄️ SQL — you can edit this query
Result
Transaction started.

1 row updated.

0 rows updated.

Transaction rolled back (undone).

┌────────────┬─────────┬──────┐
│ student_id │ month   │ paid │
├────────────┼─────────┼──────┤
│ 4          │ 2026-07 │ 0    │
└────────────┴─────────┴──────┘
(1 row)

💡 In a real app, ANY error inside a transaction (a broken CHECK, a lost connection) also leads to ROLLBACK.

What does ACID mean in SQL transactions?

Atomicity: all steps happen or none do. Consistency: rules like CHECK (balance >= 0) are true before and after. Isolation: two payments at the same time do not mix up each other's steps. Durability: after COMMIT, the change survives a power cut.

Below, a parent moves ₹2,000 from Kabir's wallet to his cousin Aman's. The total money in all wallets must be the same before and after. That is consistency you can check.

Try it: change 2000 in the first UPDATE to 2500 only. The totals no longer match: a bug a transaction cannot catch for you.

📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
🗄️ SQL — you can edit this query
Result
┌──────────────┐
│ total_before │
├──────────────┤
│ 18000        │
└──────────────┘
(1 row)

Transaction started.

1 row updated.

1 row updated.

Transaction committed (saved).

┌─────────────┐
│ total_after │
├─────────────┤
│ 18000       │
└─────────────┘
(1 row)

┌────────────┬─────────┐
│ student_id │ balance │
├────────────┼─────────┤
│ 2          │ 8000    │
│ 4          │ 3000    │
│ 6          │ 7000    │
└────────────┴─────────┘
(3 rows)

How do apps use transactions with MySQL or SQLite?

Apps rarely type BEGIN by hand. The program starts a transaction, runs the steps with ? placeholders (Lesson 6), and then commits. If anything goes wrong, it rolls back.

Here is the pattern in Python with SQLite. MySQL works the same way; there you may also see START TRANSACTION, which means the same as BEGIN.

By default, most databases run in autocommit mode: each single statement is its own tiny transaction. BEGIN switches that off until COMMIT or ROLLBACK.

📄 Example to read — Python app code — shown, not run here
try:
    db.execute("BEGIN")
    db.execute("UPDATE fees SET paid = 1 WHERE student_id = ? AND month = ?", (sid, month))
    cur = db.execute("UPDATE wallets SET balance = balance - ? WHERE student_id = ? AND balance >= ?",
                     (amount, sid, amount))
    if cur.rowcount == 0:
        raise ValueError("Not enough money in the wallet")
    db.execute("INSERT INTO receipts (student_id, month, amount) VALUES (?, ?, ?)", (sid, month, amount))
    db.execute("COMMIT")
except Exception:
    db.execute("ROLLBACK")

🤔 Predict the output

Think like SQL: read the code and choose what it prints. You get the reason straight away.

Question 1. Kabir's wallet starts at 9000. What is the balance at the end?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
BEGIN;
UPDATE wallets SET balance = balance - 1000 WHERE student_id = 6;
ROLLBACK;

SELECT balance FROM wallets WHERE student_id = 6;

Question 2. Aman's wallet starts at 6000. What is the balance at the end?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
UPDATE wallets SET balance = balance - 500 WHERE student_id = 2;

BEGIN;
UPDATE wallets SET balance = balance - 500 WHERE student_id = 2;
ROLLBACK;

SELECT balance FROM wallets WHERE student_id = 2;

Question 3. What does the last query return?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
BEGIN;
INSERT INTO receipts (student_id, month, amount) VALUES (2, '2026-07', 4500);
INSERT INTO receipts (student_id, month, amount) VALUES (4, '2026-07', 4000);
COMMIT;

SELECT COUNT(*), SUM(amount) FROM receipts;

🧩 Your challenge

Pay Kabir Singh's (student 6) June fee of ₹4,500 from his wallet in one transaction: take the money (only if the balance is enough), mark the fee paid and add a receipt. Then show his balance, the fee's paid value and the receipt. Expected: balance 4500, paid 1, one receipt for 2026-06 of 4500.

Expected result :
Transaction started.

1 row updated.

1 row updated.

1 row inserted.

Transaction committed (saved).

┌─────────┐
│ balance │
├─────────┤
│ 4500    │
└─────────┘
(1 row)

┌──────┐
│ paid │
├──────┤
│ 1    │
└──────┘
(1 row)

┌────────────┬─────────┬────────┐
│ student_id │ month   │ amount │
├────────────┼─────────┼────────┤
│ 6          │ 2026-06 │ 4500   │
└────────────┴─────────┴────────┘
(1 row)
📋 Tables in this example: students, subjects, teachers, marks, fees, wallets, receipts — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
CREATE TABLE wallets (
  student_id INTEGER PRIMARY KEY REFERENCES students(id),
  balance    INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO wallets VALUES (2, 6000), (4, 3000), (6, 9000);
CREATE TABLE receipts (
  id         INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL
);
🗄️ SQL — you can edit this query

⚠️ Common mistakes (and how to fix them)

❌ Wrong

BEGIN;
UPDATE fees SET paid = 1 WHERE student_id = 2 AND month = '2026-07';
BEGIN;
UPDATE wallets SET balance = balance - 4500 WHERE student_id = 2;
COMMIT;

Error in statement 3 (BEGIN): cannot start a transaction within a transaction

✅ Right

BEGIN;
UPDATE fees SET paid = 1 WHERE student_id = 2 AND month = '2026-07';
UPDATE wallets SET balance = balance - 4500 WHERE student_id = 2;
COMMIT;

You cannot start a transaction inside another one. One BEGIN covers every step until COMMIT.

❌ Wrong

BEGIN;
UPDATE wallets SET balance = 0 WHERE student_id = 6;
COMMIT;
ROLLBACK;

Error in statement 4 (ROLLBACK): cannot rollback - no transaction is active

✅ Right

BEGIN;
UPDATE wallets SET balance = 0 WHERE student_id = 6;
ROLLBACK;

SELECT balance FROM wallets WHERE student_id = 6;

After COMMIT the transaction is over and the change is permanent. ROLLBACK must come before COMMIT, not after.

❌ Wrong

BEGIN;
UPDATE wallets SET balance = balance - 4000
WHERE student_id = 4;

Error in statement 2 (UPDATE): CHECK constraint failed: balance >= 0

✅ Right

BEGIN;
UPDATE wallets SET balance = balance - 4000
WHERE student_id = 4 AND balance >= 4000;
ROLLBACK;

Dev has only 3000, so the balance would go below 0 and the CHECK stops the statement with an error. Check the balance in the WHERE, then roll back when 0 rows change.

📌 Remember

  • A transaction is a group of statements that succeed together or not at all.
  • BEGIN starts it, COMMIT saves it, ROLLBACK undoes everything since BEGIN.
  • Without BEGIN, each statement is saved on its own at once (autocommit).
  • Check conditions inside the statement (AND balance >= amount) and roll back when a step fails.
  • ACID = Atomicity, Consistency, Isolation, Durability.

Finished the quiz and the challenge?

Questions students ask

What happens if I forget COMMIT?

The changes stay unfinished. If the connection closes, the database rolls them back, so they are lost. Other users may also be blocked while your transaction is open. Always end with COMMIT or ROLLBACK.

Is START TRANSACTION the same as BEGIN?

Yes. MySQL and PostgreSQL accept START TRANSACTION; SQLite and PostgreSQL accept BEGIN. Both start a transaction that ends with COMMIT or ROLLBACK.

What is a savepoint in SQL?

A savepoint is a marker inside a transaction. ROLLBACK TO name undoes only the work after that marker, and the transaction goes on. SQLite, MySQL and PostgreSQL all support SAVEPOINT.

Do transactions make my queries slower?

Usually not much, and many inserts inside one transaction are often faster than separate ones. The real cost is holding a transaction open for long, which can make other users wait.