Skip to main content

What is a lock in SQL?

A lock in SQL is a mechanism that restricts other transactions' access to data, to preserve integrity and avoid conflicts during concurrent operations.

When one transaction reads or changes a row, table, or memory page, the database can lock that object, preventing other transactions from changing it until the current one finishes.

The main lock types

  • A shared lock allows other transactions to read the data, but not change it.
  • An exclusive lock fully blocks access: no one can read or change it until the lock is released.

Example: if one transaction is updating a user's balance, a second one can't change the same data until the first finishes (COMMIT or ROLLBACK). This prevents race conditions and data corruption.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.