Skip to the page
Tecyfy

SQL interview question · hard

Imagine two users simultaneously try to update the same row in a table. How can you prevent the "lost update" problem where one user's changes overwrite the other's?

The short answerA lost update happens when two transactions read the same row, each computes a new value, and the second write silently replaces the first.

The full answer

A lost update happens when two transactions read the same row, each computes a new value, and the second write silently replaces the first. Prevent it by making the update atomic, by detecting conflicts with a version check (optimistic locking), or by locking the row while you work on it (pessimistic locking). Stricter isolation levels also detect it in some databases.

1. Atomic updates: often the whole answer If the new value can be computed from the old one, let the database do it in one statement, instead of reading into the application and writing back:

UPDATE products SET stock = stock - 1 WHERE product_id = 42 AND stock > 0;

Concurrent updates on the same row are queued by the database, so no decrement is lost.

2. Optimistic locking (version check) Store a version number, read it with the row, and update only if it hasn't changed:

-- read
SELECT name, email, version FROM users WHERE user_id = 7;      -- version = 3

-- write
UPDATE users
SET email = 'new@example.com', version = version + 1
WHERE user_id = 7 AND version = 3;
-- 0 rows updated means someone else changed the row: reload and retry, or show the conflict
  • No locks are held while a user edits a form, so it suits web applications where conflicts are rare.
  • The application must check the affected row count and handle conflicts.
  • Many ORMs support it directly (a version column in JPA, Entity Framework or Django packages); SQL Server's rowversion column serves the same purpose.

3. Pessimistic locking Lock the row when reading it, inside a transaction:

BEGIN;
SELECT balance FROM accounts WHERE account_id = 1 FOR UPDATE;   -- others wait here
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
COMMIT;
  • Guarantees no conflict, so it suits short, contended operations such as money movements.
  • Others wait while the lock is held, so keep transactions short and never hold a lock across user think-time.
  • Lock rows in a consistent order to avoid deadlocks, and set a lock timeout; NOWAIT fails at once instead of waiting, and SKIP LOCKED suits job queues.
  • SQL Server uses WITH (UPDLOCK, ROWLOCK) hints for the same effect.

4. Isolation levels

  • In PostgreSQL, a REPEATABLE READ or SERIALIZABLE transaction that tries to update a row another transaction changed and committed fails with a serialization error, so the lost update becomes a retry.
  • The default READ COMMITTED level in most databases doesn't stop read-then-write lost updates on its own.
  • Higher isolation means more aborted transactions, so the application needs retry logic.

Choosing Use an atomic update when possible; optimistic locking for user-facing edits; SELECT ... FOR UPDATE for short, high-value operations; stricter isolation where the database supports it and retries are in place.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1SELECT
  2. 2Transaction isolation