Select, Filter and Sort · Problem 9 of 118

Open Accounts: Savings or Rich

MediumSelect, Filter and SortBank data

The bank wants to offer a new product to open accounts that are either savings accounts or hold a balance of at least 300,000 cents.

Return the account_id, account_type and balance_cents of those accounts. Closed and frozen accounts must not appear.

Hint 1

AND is evaluated before OR. Brackets change the order.

Reference solution

SELECT account_id, account_type, balance_cents
FROM accounts
WHERE status = 'open'
  AND (account_type = 'savings' OR balance_cents >= 300000);

AND binds tighter than OR, exactly like multiplication binds tighter than addition. Without brackets the wrong query reads as (open AND savings) OR rich, which lets rich closed accounts in. Whenever a condition mixes AND and OR, add brackets even if you think you know the precedence: the next reader will thank you.