Bird
0
0

You execute this PostgreSQL code:

medium📝 Debug Q6 of 15
PostgreSQL - Transactions and Concurrency
You execute this PostgreSQL code:
BEGIN;
UPDATE wallets SET amount = amount - 50 WHERE id = 2;
SELECT amount FROM wallets WHERE id = 2;
ROLLBACK;
SELECT amount FROM wallets WHERE id = 2;

What value does the second SELECT return?
AThe decreased amount after update
BThe original amount before the update
CNULL because the transaction was rolled back
DAn error due to rollback
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction effects

    The UPDATE reduces amount but is not committed.
  2. Step 2: Effect of ROLLBACK

    ROLLBACK undoes the update, restoring original data.
  3. Step 3: Final SELECT after rollback

    Returns original amount as update was discarded.
  4. Final Answer:

    The original amount before the update -> Option B
  5. Quick Check:

    Rollback restores original data [OK]
Quick Trick: Rollback discards uncommitted changes [OK]
Common Mistakes:
  • Expecting updated value after rollback
  • Assuming rollback causes NULL result
  • Thinking rollback triggers error on SELECT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes