Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 144/ 160
08. Transactions, Security & Production SQL Administration

Transaction Isolation Levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable)

Choose isolation levels that balance correctness and throughput.

IntermediateRead Committed · Repeatable Read3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

It's a setting for how much one transaction is protected from others changing data at the same time. More protection means fewer surprises, but more waiting or retrying.

Think of it like…

Editing a shared document: Read Uncommitted shows other people's half-typed sentences; Read Committed shows only saved versions, but they can change between your glances; Repeatable Read freezes your view of the document when you start; Serializable makes it as if everyone took turns.

Where you’ve seen it

Two users book the last seat. Under Read Committed both read seats_left = 1, both decrement it, and the flight is overbooked. Fixes: SELECT … FOR UPDATE, an atomic UPDATE … SET seats_left = seats_left - 1 WHERE seats_left > 0, or Serializable with retries.

Words you’ll hear

Dirty read
Reading another transaction's uncommitted change.
Non-repeatable read
Reading the same row twice in one transaction and getting different values.
Phantom read
Re-running a range query and getting new rows.
Serialization failure
An error (SQLSTATE 40001) that the app must handle by retrying the transaction.

Visual diagram

Loading interactive diagram

Theory & example queries

The anomalies each level prevents

Read Uncommitted allows dirty reads. Read Committed prevents them, but each statement sees a fresh snapshot, so reading a row twice in one transaction can give different values (a non-repeatable read) and repeating a range query can return new rows (a phantom).

Repeatable Read gives the whole transaction one consistent snapshot, so values don't change underneath you; in PostgreSQL it also prevents phantoms. Serializable guarantees the outcome equals some one-at-a-time ordering of the transactions, which also rules out write skew.

Setting it for one transactionQuery example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sqlReadable preview
1BEGIN ISOLATION LEVEL REPEATABLE READ;2SELECT …;3UPDATE …;4COMMIT;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Start transaction

Open a transaction so changes can be checked and committed or rolled back together.

1/4
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative transaction sequence
BEGIN SELECT: 1 representative row from the transaction snapshot UPDATE 1 COMMIT

Note: The ellipses stand for a concrete workload. At REPEATABLE READ, the exact rows and affected count depend on that workload; the sample shows one successful UPDATE.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: A higher level means fewer anomalies and more waiting or retries.

What databases actually do

Defaults differ: PostgreSQL, SQL Server and Oracle use Read Committed; MySQL InnoDB uses Repeatable Read. PostgreSQL implements Repeatable Read and Serializable with snapshots (MVCC), so instead of blocking it aborts a conflicting transaction with a serialization error, which your code must retry.

A higher isolation level isn't the only tool. For a single hot row, SELECT … FOR UPDATE or an atomic conditional UPDATE usually fixes the race at Read Committed with less overhead than switching the whole app to Serializable.

A race-free decrement at Read CommittedQuery example
1Input dataStart from the source rows or state that the statement sees.
This statement works with the current schema, configuration, or database state rather than a row dataset. The command still follows the same input → query → result flow.
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1UPDATE flights2SET3  seats_left = seats_left - 14WHERE5  id = 426  AND seats_left > 0;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/6
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative atomic decrement
UPDATE 1

Note: Assumes flight 42 has a seat available. A concurrent or already sold-out attempt reports UPDATE 0, which is the sold-out signal.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

then check the affected row count: 0 means sold out.

Key takeaway: Know your database's default, and treat Serializable as "be ready to retry".

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Analysis

Name the anomaly you're worried about

Is it a dirty read, a value changing mid-transaction, new rows appearing, or two transactions overwriting each other (a lost update or write skew)?

Focus at this step

two users, same row, same moment

What to watch at this step
Draw
a two-transaction timeline
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

Isolation levels control what a transaction can see of other transactions running at the same time. The SQL standard defines four: Read Uncommitted, Read Committed, Repeatable Read and Serializable. Each one prevents more anomalies (dirty reads, non-repeatable reads, phantoms) at the cost of more blocking or retries.

Why it matters: Many concurrency bugs (double-booked seats, lost balance updates, reports that don't add up) come from running at a weaker isolation level than the code assumes. Interviewers ask about it to see whether you can reason about two users hitting the same row at the same moment.

When to reach for it

  • Booking, inventory and balance updates where two users can collide
  • Reports that read several tables and must be internally consistent
  • Explaining why a "check, then insert" pattern is racy

When to avoid or be careful

  • Switching the whole app to Serializable without retry logic
  • Read Uncommitted or NOLOCK for anything financial
  • Assuming the same level behaves identically in MySQL and PostgreSQL

The four isolation levels

Read Uncommitted
Best for: Rough monitoring queries where accuracy doesn't matter (SQL Server NOLOCK)

Strengths: Never waits for writers.

Watch out: Dirty reads: you can see data that is later rolled back.

Read Committed
Best for: The default for most web applications

Strengths: No dirty reads, little blocking.

Watch out: Non-repeatable reads, phantoms and lost updates are still possible.

Repeatable Read
Best for: Reports and multi-step reads that need one consistent snapshot

Strengths: Values don't change mid-transaction (and no phantoms in PostgreSQL).

Watch out: Write skew is still possible; in PostgreSQL, conflicting updates fail and need a retry.

Serializable
Best for: Business rules across rows: no double booking, on-call coverage

Strengths: Same outcome as running transactions one at a time.

Watch out: The most overhead, and the app must retry serialization failures.

Interview signals & numbers to know

When this topic comes up

  • “Two users book the last seat”
  • “What is a phantom read?”
  • Lost update
  • A race condition inside a transaction
  • “What's the default isolation level?”

Benchmarks & rules of thumb

4 -
standard isolation levels
Read Committed -
default in PostgreSQL, SQL Server, Oracle
Repeatable Read -
default in MySQL InnoDB
40001 -
serialization failure SQLSTATE: retry

Common mistakes & follow-ups

Common interview mistakes

  • Assuming a transaction alone prevents races. At Read Committed, "read, then write" is still racy.
  • Raising the isolation level without retrying serialization failures.
  • Mixing up non-repeatable reads (changed values) with phantom reads (new rows).

Likely interviewer follow-ups

  • Two transactions both read a balance of 100 and each subtract 30. What happens at Read Committed, and how do you prevent it?
  • What is write skew, and which isolation level prevents it?
  • Why does PostgreSQL return serialization errors instead of making transactions wait?
Interview tip: Define the three anomalies first, map each level to what it prevents, then solve a concrete race (last seat, double withdrawal) with the lightest fix. Mentioning the engine defaults shows real-world experience.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Transaction Isolation Levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Concurrency Anomalies (Dirty Reads, Non-Repeatable Reads, Phantom Reads, Lost Updates)
Next topic
Row-Level Locking (SELECT ... FOR UPDATE, FOR SHARE, SKIP LOCKED)