What is a Deadlock in SQL and How Can It Be Resolved or Prevented?
π‘ Concept Name
SQL Deadlock occurs when two or more transactions permanently block each other by each holding a lock the other needs.
π Quick Intro
Deadlocks are a concurrency issue in SQL databases where multiple transactions wait on each other to release resources, leading to a situation where none can proceed unless one is forcibly terminated.
π§ Analogy / Short Story
Imagine two people in a narrow hallwayβone holding a key the other needs to open a door, and vice versa. Neither can move forward or backward unless one gives up. Thatβs a deadlock: mutual waiting with no progress.
π§ Technical Explanation
- π Deadlocks happen when transactions lock resources in different orders.
- π Each transaction waits for a resource that another transaction holds, forming a circular wait.
- π§ SQL Server and other DBMS detect deadlocks and terminate one transaction to break the cycle.
- π Tools like SQL Server Profiler or Extended Events help trace deadlocks.
π― Purpose & Use Case
- β Understand deadlocks to avoid concurrency bottlenecks in transactional systems.
- β Improve locking strategies and index usage in multi-user environments.
π» Real Code Example
-- Transaction A
BEGIN TRAN;
UPDATE Products SET Stock = Stock - 1 WHERE ProductID = 1;
WAITFOR DELAY '00:00:05'; -- Simulate delay
UPDATE Orders SET Quantity = Quantity + 1 WHERE OrderID = 101;
COMMIT;
-- Transaction B (running concurrently)
BEGIN TRAN;
UPDATE Orders SET Quantity = Quantity + 1 WHERE OrderID = 101;
WAITFOR DELAY '00:00:05'; -- Simulate delay
UPDATE Products SET Stock = Stock - 1 WHERE ProductID = 1;
COMMIT;

β Interview Q&A
Q1: What is a deadlock in SQL?
A: Itβs a condition where two or more transactions wait indefinitely for each otherβs locked resources.
Q2: Why do deadlocks occur?
A: They occur due to circular wait conditions when transactions lock resources in inconsistent orders.
Q3: How does SQL Server handle deadlocks?
A: It automatically detects and kills one transaction (the victim) to break the cycle.
Q4: How can we detect deadlocks?
A: Using tools like SQL Server Profiler, Extended Events, or error logs with deadlock graphs.
Q5: How can we prevent deadlocks?
A: Access tables in the same order across transactions, reduce transaction scope, and use appropriate isolation levels.
π MCQs
Q1. What causes a deadlock in SQL?
- Syntax error
- Slow queries
- Circular wait on resources
- Missing indexes
Q2. What does SQL Server do when a deadlock is detected?
- Rolls back both
- Ignores it
- Kills one transaction
- Retries both
Q3. How to avoid deadlocks?
- Use SELECT only
- Lock all rows
- Access resources in same order
- Avoid transactions
Q4. Which tool helps detect deadlocks?
- SSMS Themes
- Query Editor
- SQL Profiler
- IntelliSense
Q5. What is a good practice to reduce deadlocks?
- Use complex joins
- Avoid indexes
- Keep transactions short
- Delay commits
π‘ Bonus Insight
Deadlocks are not always avoidable, but understanding locking behavior and isolating critical sections of code can drastically reduce their chances.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!