Exercise · beginner · 15 min
Referential integrity with explicit exceptions
Payments must point at a known account. Quarantine missing and unknown references with distinct reasons so every row is accounted for.
The rules
These rules decide what a correct result is. Your answer is checked on this dataset and on hidden datasets that follow the same rules.
Inputs
payments(id, account, amount_cents) — text. id is unique. amount_cents is always a valid whole number (refunds are negative).
accounts(account, name) — text. Accounts are trimmed and unique, never missing.
Rules
account: remove ASCII whitespace at both ends; an empty result is missing.
Quarantine each invalid payment with the FIRST failing reason:
1. missing_account account is missing
2. unknown_account account has no matching row in accounts
Valid payments get the account's name.
Outputs (row order does not matter)
clean(id text, account text, name text, amount_cents int64)
exceptions(id text, reason text)The data
payments · 10 rows
| id | account | amount_cents |
|---|---|---|
| pay59hn | 999 | 51974 |
| payij5o | 4 | 57923 |
| payfhun | 7 | 77641 |
| paym2n8 | missing | 72752 |
| paywa3o | 999 | -1746 |
| pay7u1w | ·7· | -1489 |
| pay77t1 | 999 | -3521 |
| payacbh | 48 | 80852 |
| payngmn | 7 | 87639 |
| payfkj9 | 999 | -461 |
accounts · 5 rows
| account | name |
|---|---|
| 12 | Bolt GmbH |
| 7 | Delta Co |
| 4 | Cedar SA |
| 22 | Acme Ltd |
| 48 | Echo LLC |
What you practise
- lookups
- quarantine
- reconciliation
Your answer is also checked on hidden datasets generated from the same rules.
Sign in to bookmark this and keep private notes.