Changing Data · Problem 111 of 118

Freeze Small Open Accounts

MediumChanging DataBank data

Set status to 'frozen' for every account that is currently open and has a balance_cents below 20,000. Accounts that are already closed or frozen must not change.

Check query. After your statements run, this query is run on the changed data and its result is compared with the expected one:

SELECT status, COUNT(*) AS accounts
FROM accounts
GROUP BY status
ORDER BY status;
Hint 1

The condition must say both things: the account is open and the balance is small.

Reference solution

UPDATE accounts
SET status = 'frozen'
WHERE status = 'open'
  AND balance_cents < 20000;

The business rule had two parts, and the WHERE must have both. A closed account is closed for a reason; turning it into "frozen" would reopen a case. Conditions on the current state (status = 'open') protect rows that should not move. State changes are best written as "from this state, under this condition, to that state".