Interview Q&A · SQL · 10 questions in this topic

Transactions & Concurrency — SQL Interview Q&A

All SQL topics

Correct concurrent writes — ACID, isolation levels, locking and the lost-update defenses. Part of Indexes & Transactions · 192 questions in the SQL bank.

What is a transaction (BEGIN / COMMIT / ROLLBACK)?

Answer

A transaction bundles statements into one atomic unit: all commit together (COMMIT) or all roll back (ROLLBACK) — no partial writes. BEGIN starts explicitly (autocommit otherwise commits per statement); SAVEPOINTs allow partial rollbacks inside.

Multi-statement writes without transactions are partial-failure bugs by construction.

Example

Example.sqlsql
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;  -- both or neither

💡 All-or-nothing is the atomicity sentence.

What does ACID mean?

Answer

Atomicity (all-or-nothing commits), Consistency (constraints hold before and after), Isolation (concurrent transactions do not see each other's partial work), Durability (committed data survives crashes via WAL). Together they make concurrent writes sane.

One letter per guarantee with a one-line example each is the complete answer.

Key points

  1. 1Atomicity — COMMIT all or ROLLBACK all.
  2. 2Consistency — invariants preserved.
  3. 3Isolation — no partial visibility.
  4. 4Durability — WAL-backed survival.

What are the isolation levels (READ COMMITTED to SERIALIZABLE)?

Answer

READ UNCOMMITTED (dirty reads — mostly theoretical in PostgreSQL), READ COMMITTED (no dirty reads; default in PostgreSQL), REPEATABLE READ (snapshot-stable reads; snapshot isolation in PostgreSQL), SERIALIZABLE (serial-order guarantee, aborts on conflicts). Higher isolation costs concurrency and retries.

Anomalies prevented per level (dirty/non-repeatable/phantom) is the comparison grid.

Key points

  1. 1READ COMMITTED — statement snapshots, default.
  2. 2REPEATABLE READ — transaction snapshot.
  3. 3SERIALIZABLE — serial equivalence, retry on abort.

Example

Example.sqlsql
-- PostgreSQL:
BEGIN ISOLATION LEVEL REPEATABLE READ;

💡 Anomaly-per-level is the grid interviewers expect.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits