We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium sql problem, graded against 4 test cases (2 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
SELECT
COALESCE(a.account_id, b.account_id) AS account_id,
COALESCE(a.revenue_a, 0) AS revenue_a,
COALESCE(b.revenue_b, 0) AS revenue_b,
ABS(COALESCE(a.revenue_a, 0) - COALESCE(b.revenue_b, 0)) AS diff
FROM SystemA a
FULL OUTER JOIN SystemB b ON a.account_id = b.account_id
ORDER BY account_id;
```
`COALESCE` substitutes `0` for any side that's NULL, and the joined `account_id` is taken from whichever side has it.
MySQL workaround (no FULL JOIN):
```sql
SELECT a.account_id, a.revenue_a, COALESCE(b.revenue_b, 0) AS revenue_b ...
FROM A LEFT JOIN B ON ...
UNION
SELECT b.account_id, COALESCE(a.revenue_a, 0), b.revenue_b ...
FROM A RIGHT JOIN B ON ...
```
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.