runtime
62 / 88

lesson 62 Database engineering · advanced

Transactions & savepoint recovery

Commit changes together or recover with savepoints.

the idea

A transaction groups changes so they succeed or fail together. BEGIN starts it, COMMIT keeps the changes and ROLLBACK undoes everything since BEGIN. A SAVEPOINT marks a point inside the transaction; ROLLBACK TO that savepoint undoes only the later changes.

Dataset: Finance / Payments

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

accounts7 columns · 2,109 rows
account_idnumber · unique idcustomer_idnumber · links to customersaccount_typetextopened_atdateclosed_attextcredit_limit_centsnumberopening_balance_centsnumber
11checking2022-02-24NULLNULL43125
22checking2025-06-11NULLNULL0
33checking2024-07-07NULLNULL216925
43savings2024-10-07NULLNULL475941
53credit_card2024-09-26NULL1020600-339148
64checking2023-07-18NULLNULL155995
account_idnumber · unique id
1
customer_idnumber · links to customers
1
account_typetext
checking
opened_atdate
2022-02-24
closed_attext
NULL
credit_limit_centsnumber
NULL
opening_balance_centsnumber
43125
account_idnumber · unique id
2
customer_idnumber · links to customers
2
account_typetext
checking
opened_atdate
2025-06-11
closed_attext
NULL
credit_limit_centsnumber
NULL
opening_balance_centsnumber
0
account_idnumber · unique id
3
customer_idnumber · links to customers
3
account_typetext
checking
opened_atdate
2024-07-07
closed_attext
NULL
credit_limit_centsnumber
NULL
opening_balance_centsnumber
216925
account_idnumber · unique id
4
customer_idnumber · links to customers
3
account_typetext
savings
opened_atdate
2024-10-07
closed_attext
NULL
credit_limit_centsnumber
NULL
opening_balance_centsnumber
475941
account_idnumber · unique id
5
customer_idnumber · links to customers
3
account_typetext
credit_card
opened_atdate
2024-09-26
closed_attext
NULL
credit_limit_centsnumber
1020600
opening_balance_centsnumber
-339148
account_idnumber · unique id
6
customer_idnumber · links to customers
4
account_typetext
checking
opened_atdate
2023-07-18
closed_attext
NULL
credit_limit_centsnumber
NULL
opening_balance_centsnumber
155995

first 6 of 2,109 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Roll back an entire transaction. BEGIN adds 100 cents to account 1, then ROLLBACK throws that change away—you should see the original opening_balance_cents again.the pattern

    example.sql
    BEGIN;
    UPDATE accounts
    SET opening_balance_cents = opening_balance_cents + 100
    WHERE account_id = 1;
    ROLLBACK;
    SELECT account_id, opening_balance_cents
    FROM accounts
    WHERE account_id = 1;
  2. Undo only after a savepoint. The first UPDATE on account 3 stays; ROLLBACK TO the savepoint drops the zero-out, and COMMIT keeps the earlier change.

    step-2.sql
    BEGIN;
    UPDATE accounts
    SET opening_balance_cents = opening_balance_cents + 1
    WHERE account_id = 3;
    SAVEPOINT s;
    UPDATE accounts
    SET opening_balance_cents = 0
    WHERE account_id = 3;
    ROLLBACK TO s;
    COMMIT;
    SELECT account_id, opening_balance_cents
    FROM accounts
    WHERE account_id = 3;

common mistake

ROLLBACK TO a savepoint does not end the transaction; you still need COMMIT to keep the earlier changes.

challenge 1

Begin a transaction, add 10000 cents to opening_balance_cents of accounts 1–5, then roll back.

Return account_id and opening_balance_cents for accounts 1–5 (the original values).

what to return

Columns
account_idopening_balance_cents in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Transactions & savepoint recovery — Database engineering · Runtime