Bird
Raised Fist0
SQLquery~20 mins

Read phenomena (dirty reads, phantom reads) in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Read Phenomena Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What output shows a dirty read scenario?
Consider two transactions:

Transaction 1 updates a row but does not commit.
Transaction 2 reads the updated row before commit.

Which query result best illustrates the dirty read phenomenon?
SQL
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- no commit yet

-- In parallel
SELECT balance FROM accounts WHERE id = 1;
ATransaction 2 reads the old balance before update.
BTransaction 2 reads a balance from a different account.
CTransaction 2 waits until Transaction 1 commits before reading.
DTransaction 2 reads the new balance even though Transaction 1 has not committed.
Attempts:
2 left
💡 Hint
Dirty reads happen when a transaction reads uncommitted changes from another transaction.
❓ query_result
intermediate
2:00remaining
Which output best illustrates a phantom read?
Transaction 1 runs a query to count rows matching a condition.
Transaction 2 inserts a new row matching that condition and commits.
Transaction 1 runs the same query again.

What is the expected result?
SQL
BEGIN TRANSACTION;
SELECT COUNT(*) FROM orders WHERE status = 'pending';
-- returns 5

-- In parallel
INSERT INTO orders (id, status) VALUES (101, 'pending');
COMMIT;

-- Back in Transaction 1
SELECT COUNT(*) FROM orders WHERE status = 'pending';
AFirst query returns 5, second query returns 6 rows.
BBoth queries return 5 rows.
CBoth queries return 6 rows.
DSecond query returns fewer rows than the first.
Attempts:
2 left
💡 Hint
Phantom reads happen when new rows appear in repeated queries within the same transaction.
🧠 Conceptual
advanced
2:00remaining
Which isolation level prevents dirty reads but allows phantom reads?
Choose the SQL transaction isolation level that stops dirty reads but still permits phantom reads.
AREAD UNCOMMITTED
BSERIALIZABLE
CREAD COMMITTED
DREPEATABLE READ
Attempts:
2 left
💡 Hint
Think about which isolation level blocks reading uncommitted data but does not lock range scans.
📝 Syntax
advanced
2:00remaining
Which SQL snippet correctly sets the isolation level to prevent phantom reads?
You want to set the transaction isolation level to SERIALIZABLE to avoid phantom reads.
Which SQL command is correct?
ASET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BSET ISOLATION LEVEL TRANSACTION SERIALIZABLE;
CSET TRANSACTION LEVEL ISOLATION SERIALIZABLE;
DSET ISOLATION SERIALIZABLE TRANSACTION LEVEL;
Attempts:
2 left
💡 Hint
The correct syntax follows the pattern: SET TRANSACTION ISOLATION LEVEL ...
🔧 Debug
expert
3:00remaining
Why does this transaction still experience phantom reads despite using REPEATABLE READ?
Transaction 1:
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT * FROM products WHERE category = 'books';
-- returns 10 rows

Transaction 2:
INSERT INTO products (id, category) VALUES (101, 'books');
COMMIT;

Transaction 1:
SELECT * FROM products WHERE category = 'books';
-- returns 11 rows

Why does Transaction 1 see the new row?
AThe isolation level was not set correctly before the transaction started.
BREPEATABLE READ does not prevent phantom reads in all databases.
CTransaction 1 did not lock the table explicitly.
DThe new row was inserted with a different category.
Attempts:
2 left
💡 Hint
Some databases implement REPEATABLE READ differently regarding phantom reads.

Practice

(1/5)
1. Which of the following best describes a dirty read in database transactions?
easy
A. Reading data that has been modified but not yet committed by another transaction.
B. Seeing new rows appear in a query result after re-running the same query.
C. Reading only committed data and never uncommitted changes.
D. Locking rows to prevent other transactions from reading them.

Solution

  1. Step 1: Understand dirty read definition

    A dirty read happens when a transaction reads data that another transaction has changed but not committed yet.
  2. Step 2: Compare options to definition

    Reading data that has been modified but not yet committed by another transaction. matches this exactly. Seeing new rows appear in a query result after re-running the same query. describes phantom reads, C describes a safe read, and D is about locking, not dirty reads.
  3. Final Answer:

    Reading data that has been modified but not yet committed by another transaction. -> Option A
  4. Quick Check:

    Dirty read = reading uncommitted data [OK]
Hint: Dirty read = reading uncommitted changes [OK]
Common Mistakes:
  • Confusing dirty read with phantom read
  • Thinking dirty read only reads committed data
  • Mixing locking with read phenomena
2. Which SQL transaction isolation level allows dirty reads?
easy
A. SERIALIZABLE
B. REPEATABLE READ
C. READ COMMITTED
D. READ UNCOMMITTED

Solution

  1. Step 1: Recall isolation levels and dirty reads

    Only the READ UNCOMMITTED isolation level allows dirty reads, meaning it can read uncommitted changes.
  2. Step 2: Match options to isolation levels

    READ COMMITTED and higher levels prevent dirty reads. SERIALIZABLE is the strictest. So, READ UNCOMMITTED is correct.
  3. Final Answer:

    READ UNCOMMITTED -> Option D
  4. Quick Check:

    Dirty reads allowed only in READ UNCOMMITTED [OK]
Hint: Dirty reads only happen at READ UNCOMMITTED level [OK]
Common Mistakes:
  • Choosing READ COMMITTED as allowing dirty reads
  • Confusing phantom reads with dirty reads
  • Not knowing isolation level hierarchy
3. Consider two transactions running concurrently on a table Orders. Transaction 1 reads all rows where status = 'pending'. Transaction 2 inserts a new row with status = 'pending' and commits. If Transaction 1 repeats the read, what phenomenon might it observe?
medium
A. Dirty read
B. Non-repeatable read
C. Phantom read
D. Lost update

Solution

  1. Step 1: Analyze the scenario with inserted rows

    Transaction 1 reads rows with status 'pending'. Transaction 2 inserts a new 'pending' row and commits.
  2. Step 2: Identify the phenomenon on repeated read

    When Transaction 1 reads again, it sees the new row that wasn't there before. This is a phantom read, where new rows appear between reads.
  3. Final Answer:

    Phantom read -> Option C
  4. Quick Check:

    New rows appearing = Phantom read [OK]
Hint: New rows between reads = phantom read [OK]
Common Mistakes:
  • Confusing phantom read with dirty read
  • Thinking non-repeatable read applies to new rows
  • Mixing lost update with read phenomena
4. A developer wrote this SQL transaction code but observes inconsistent reads. Identify the error related to read phenomena:
BEGIN TRANSACTION;
SELECT * FROM Products WHERE price > 100;
-- Another transaction inserts a new product with price 150 and commits
SELECT * FROM Products WHERE price > 100;
COMMIT;

What is the likely cause?
medium
A. Using READ UNCOMMITTED isolation level causing dirty reads
B. Using READ COMMITTED isolation level causing phantom reads
C. Using SERIALIZABLE isolation level causing lost updates
D. Using REPEATABLE READ isolation level causing non-repeatable reads

Solution

  1. Step 1: Understand the scenario with repeated SELECTs

    The transaction reads products with price > 100 twice. Between reads, another transaction inserts a new matching row and commits.
  2. Step 2: Identify the read phenomenon

    Because the isolation level is likely READ COMMITTED, the second read sees the new row, causing a phantom read.
  3. Final Answer:

    Using READ COMMITTED isolation level causing phantom reads -> Option B
  4. Quick Check:

    Phantom reads occur at READ COMMITTED [OK]
Hint: Phantom reads happen at READ COMMITTED level [OK]
Common Mistakes:
  • Assuming dirty reads cause this behavior
  • Confusing lost updates with read phenomena
  • Thinking REPEATABLE READ allows phantom reads
5. You want to prevent both dirty reads and phantom reads in your application. Which transaction isolation level should you choose?
hard
A. SERIALIZABLE
B. READ COMMITTED
C. REPEATABLE READ
D. READ UNCOMMITTED

Solution

  1. Step 1: Review isolation levels and phenomena prevented

    READ UNCOMMITTED allows dirty reads. READ COMMITTED prevents dirty reads but allows phantom reads. REPEATABLE READ prevents dirty and non-repeatable reads but may allow phantom reads.
  2. Step 2: Identify level preventing both dirty and phantom reads

    SERIALIZABLE is the strictest level and prevents dirty reads, non-repeatable reads, and phantom reads by fully isolating transactions.
  3. Final Answer:

    SERIALIZABLE -> Option A
  4. Quick Check:

    SERIALIZABLE prevents dirty and phantom reads [OK]
Hint: Use SERIALIZABLE to prevent dirty and phantom reads [OK]
Common Mistakes:
  • Choosing REPEATABLE READ thinking it blocks phantom reads
  • Confusing READ COMMITTED with preventing all read anomalies
  • Ignoring performance impact of SERIALIZABLE
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard