Skip to the solution
EasyDSA14 min read

When would you use a transaction explicitly?

604 views

The problem

Question presented to candidate: "Most of the time our code just runs SQL statements one by one and it works. When would you actually reach for BEGIN and COMMIT yourself? Give me real cases from an app like an online shop, and tell me when you would avoid a transaction."

What a strong answer should cover:

  • By default each statement runs in its own transaction (autocommit), so a single statement is already atomic; an explicit transaction is for several statements that must succeed or fail together (order + items + stock)
  • Business-rule failures: check the row count of a conditional update and ROLLBACK when the rule fails, so no half-written order remains
  • Performance: many small writes in one transaction avoid one disk flush per commit, so bulk loads are much faster
  • Schema migrations: PostgreSQL and SQLite can roll back DDL; MySQL commits implicitly before most DDL
  • Read-then-write and consistent reports: a transaction alone at READ COMMITTED does not stop lost updates; pair it with an atomic UPDATE, SELECT ... FOR UPDATE or a stronger isolation level
  • When not to: never hold a transaction open across user think time or slow network calls; keep transactions short

Clarifying questions expected:

  • Which database and driver? (Some drivers open a transaction implicitly; others autocommit.)
  • Can the operation be expressed as one statement, or does it span several tables?

Code / implementation expected: Yes β€” an order placed as one transaction, and a rollback when a stock check fails.

Schema used by the examples:

  • truncate-vs-delete

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

14 min read Β· 13 sections

Run the code

Edit the query and run it against the tables for this question.

SQLPlace an order as one unit: order row, item rows and stock decrements commit together
SQLA stock check fails inside the transaction, so the app rolls everything back
PostgreSQL: a migration inside a transaction rolls back completely (transactional DDL)
BEGIN;
CREATE TABLE coupons (code TEXT PRIMARY KEY, percent_off INTEGER);
ALTER TABLE products ADD COLUMN sku TEXT;
ROLLBACK;
SELECT to_regclass('coupons') IS NULL AS coupons_table_gone,
       (SELECT COUNT(*) FROM information_schema.columns
        WHERE table_name = 'products' AND column_name = 'sku') AS sku_columns;

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 9 of 82 in SQL

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All SQL questions
Up nextEasy

What are NOT NULL, DEFAULT and other column constraints?

Continue