CS FundamentalsModerateTCS NQT · Infosys pattern

A query uses `SELECT ... FOR UPDATE` to explicitly lock the rows it reads within a transaction. What is the main purpose of this, compared to a plain SELECT under READ COMMITTED?

  1. A. It makes the SELECT run faster
  2. B. It acquires an exclusive (or update) lock on the selected rows immediately, preventing other transactions from modifying (or in some systems even reading) them until this transaction commits — used to safely read-then-update a row without losing the update to a concurrent transaction
  3. C. It guarantees the query returns results in a specific sorted order
  4. D. It disables the use of any index for that query

Correct answer: B. It acquires an exclusive (or update) lock on the selected rows immediately, preventing other transactions from modifying (or in some systems even reading) them until this transaction commits — used to safely read-then-update a row without losing the update to a concurrent transaction

SELECT ... FOR UPDATE is the standard way to avoid the lost-update anomaly under weaker isolation levels: by locking the rows as soon as they are read, no other transaction can sneak in and modify them before this transaction performs its own update based on what it read, closing the window a plain SELECT would otherwise leave open.

More DBMS and SQL questions

Exams that ask this topic