Debit only if there's enough money
An account withdrawal should go through only if there's enough money. If you do «read balance → check → write» as separate statements, between the steps another transaction can drain the balance and our operation pushes the account negative.
Solve with a single update with the condition in the filter. Deduct 200 from the account with id 1, but only if the current balance is at least 200. If there's not enough money the update touches zero rows, and on the backend you return «insufficient funds» to the user.
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in