Net Transfer Flow
A transfer moves money from from_account to to_account. Considering only transfers with status = 'done', the net flow of an account is the total it received minus the total it sent.
Return account_id and net_cents for every account that appears in at least one completed transfer, sorted by account_id.
Hint 1
Stack two result sets with UNION ALL: one with to_account and +amount, one with from_account and -amount. Then sum per account.
Reference solution
WITH movements AS (
SELECT to_account AS account_id, amount_cents AS cents FROM transfers WHERE status = 'done'
UNION ALL
SELECT from_account AS account_id, -amount_cents AS cents FROM transfers WHERE status = 'done'
)
SELECT account_id, SUM(cents) AS net_cents
FROM movements
GROUP BY account_id
ORDER BY account_id;
Two columns that play the same role (the receiving and the sending account) are awkward to aggregate. Turn them into one column of events with UNION ALL (signed amounts), then group. Use UNION ALL, not UNION: two different transfers of the same amount to the same account are two real movements, and UNION would silently merge them. Reach for UNION only when you truly want duplicates removed.