0
0
SQLquery~20 mins

Deadlock concept and prevention in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deadlock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is a deadlock in database systems?
In simple terms, what does a deadlock mean when two or more transactions are running in a database?
AA transaction is rolled back due to syntax errors in SQL.
BA transaction completes successfully without any waiting.
CTwo transactions wait forever for each other to release locks, causing a standstill.
DA database automatically increases its storage space.
Attempts:
2 left
💡 Hint
Think about what happens when two people each hold a key the other needs and neither can proceed.
query_result
intermediate
2:00remaining
Which query order can cause a deadlock?
Given two transactions:

Transaction 1 locks Table A then Table B.
Transaction 2 locks Table B then Table A.

Which option shows the order of locking that can cause a deadlock?
ATransaction 1 locks Table A, Transaction 2 locks Table B, then both wait for the other's table.
BTransaction 1 locks Table A and Table B, then Transaction 2 locks both tables.
CTransaction 1 locks Table B, Transaction 2 locks Table A, then both commit immediately.
DTransaction 1 and Transaction 2 both lock Table A first, then Table B.
Attempts:
2 left
💡 Hint
Deadlocks happen when each transaction holds a lock the other needs next.
📝 Syntax
advanced
2:00remaining
Which SQL statement helps prevent deadlocks by setting lock timeout?
Choose the correct SQL command to set a lock timeout to avoid waiting forever and reduce deadlocks.
ASET LOCK_TIMEOUT = 5000;
BSET LOCK_TIMEOUT 5000;
CSET LOCKTIMEOUT = 5000;
DSET LOCKTIMEOUT 5000;
Attempts:
2 left
💡 Hint
The correct syntax uses underscore and no equals sign.
optimization
advanced
2:00remaining
Which approach best reduces deadlocks in concurrent transactions?
You want to minimize deadlocks in your database where many transactions update multiple tables. Which approach is best?
AIncrease transaction size to hold locks longer.
BDisable all locking mechanisms to speed up transactions.
CUse random order of table access to spread locks evenly.
DAccess tables in the same order in all transactions to avoid circular waits.
Attempts:
2 left
💡 Hint
Think about how consistent order prevents waiting cycles.
🔧 Debug
expert
3:00remaining
Identify the cause of deadlock in this SQL transaction sequence
Two transactions run:

Transaction 1:
BEGIN TRAN;
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

Transaction 2:
BEGIN TRAN;
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;

What causes the deadlock here?
ABoth transactions lock accounts in opposite order causing circular wait.
BThe WAITFOR DELAY causes syntax error stopping transactions.
CTransactions update same account simultaneously without locks.
DCOMMIT statements are missing causing incomplete transactions.
Attempts:
2 left
💡 Hint
Look at the order of account updates and the delay between them.