Atomic decrement with check
SQL
Medium
4 views
Problem Description
Decrease Inventory.qty by requested amount only if enough stock. Make it atomic in one statement and run inside a transaction.
Sample Test Case
Input:
Inventory(product_id, qty)
Constraints
Use conditional update
Official Solution
BEGIN; UPDATE Inventory SET qty = qty - 3 WHERE product_id=10 AND qty >= 3; COMMIT;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!