SQL

SQL Transactions Interview Questions

5 questions with answers · SQL Interview Guide

ACID properties, isolation levels, locking, deadlocks, COMMIT, ROLLBACK, and concurrency control.

bar_chartQuick stats
Total questions5
High frequency2
With code examples2
1

What are different transaction levels in SQL SERVER

SQL Server supports five transaction isolation levels that control how transactions interact with each other. READ UNCOMMITTED allows dirty reads (seeing uncommitted changes from other transactions). READ COMMITTED (the default) only reads committed data. REPEATABLE READ prevents other transactions from modifying rows you have read. SERIALIZABLE is the strictest level, preventing phantom reads by locking entire ranges. SNAPSHOT uses row versioning to give each transaction a consistent view of the data without blocking other transactions.

2

What is ACID fundamental? What are transactions in SQL SERVER

ACID stands for Atomicity, Consistency, Isolation, and Durability. Atomicity means all operations in a transaction succeed or all are rolled back, there is no partial completion. Consistency ensures the database moves from one valid state to another, respecting all constraints and rules. Isolation means concurrent transactions do not see each other's intermediate state, controlled by isolation levels like READ COMMITTED or SERIALIZABLE. Durability guarantees that once a transaction is committed, the changes survive even a system crash, ensured by the transaction log. In SQL Server, a transaction is a unit of work wrapped in BEGIN TRANSACTION and ended with COMMIT or ROLLBACK.

sql
BEGIN TRANSACTION;
    UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;
    UPDATE accounts SET balance = balance + 500 WHERE account_id = 2;

    IF @@ERROR <> 0
        ROLLBACK;
    ELSE
        COMMIT;
3

If locking is not implemented, what issues can occur

Without locking, three classic concurrency problems can occur. First, dirty reads, where one transaction reads uncommitted data from another transaction that later gets rolled back, so you end up with data that never officially existed. Second, non-repeatable reads, where you read a row, another transaction modifies it and commits, and when you read it again in the same transaction you get a different value. Third, phantom reads, where a query returns a different set of rows when executed twice in the same transaction because another transaction inserted or deleted rows between the two reads.

4

What is LOCK escalation

Lock escalation is a process where the database engine automatically converts many fine-grained locks, like row-level or page-level locks, into a single coarser lock, typically a table-level lock. This happens to reduce the memory overhead that comes from maintaining a huge number of individual locks. SQL Server, for example, escalates to a table lock when a transaction holds roughly 5,000 locks on a single object. The downside is reduced concurrency since a table lock blocks other transactions from accessing any part of that table, so escalation can cause unexpected blocking and timeouts under heavy load.

5

Can we suggest locking hints to SQL SERVER

Yes, SQL Server allows you to provide locking hints as part of a query to override the default locking behavior. You add them in the FROM clause using the WITH keyword. Common hints include NOLOCK which reads without acquiring shared locks (dirty reads are possible), ROWLOCK which forces row-level locking, TABLOCK which forces a table-level lock, UPDLOCK which takes update locks during a read, and HOLDLOCK which holds a shared lock until the transaction ends. These hints should be used carefully because they can improve performance in specific scenarios but can also introduce dirty reads or increase contention if misused.

sql
-- NOLOCK hint: reads uncommitted data, no shared locks taken
SELECT * FROM orders WITH (NOLOCK) WHERE order_date > '2024-01-01';

-- UPDLOCK hint: prevents deadlocks in read-then-update patterns
SELECT * FROM inventory WITH (UPDLOCK) WHERE product_id = 5;

Knowing the answers is half the battle

The other half is explaining them clearly under pressure.

Try a free mock interviewarrow_forward

More SQL topics