Quick revision
The short answer
A FULL OUTER JOIN of payments and settlements on ref, keeping rows where either side is NULL or the amounts differ, finds all three kinds of break in one query, with COALESCE(p.ref, s.ref) as the reference. On 50 million rows, the payments side must be restricted to the settlement date window before the join, in a subquery so the outer join still works, and both ref columns need the same type and collation so a hash join or index can be used. Results go into a reconciliation table that people work through and alerts watch.
What to remember
In this answer, we reconciled payments with a settlement file using a FULL OUTER JOIN on ref, with COALESCE for the reference and IS DISTINCT FROM for amounts, so missing rows on either side and mismatches appear in one result. We restricted payments to the settlement window inside a subquery, so the outer join still reports settlements without payments, made sure both ref columns share type and collation for an efficient hash join, wrote the MySQL version as two anti-joins and an inner join, and stored the breaks in a table with alerts on counts and age.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A FULL OUTER JOIN of payments and settlements on ref, keeping rows where either side is NULL or the amounts differ, finds all three kinds of break in one query, with COALESCE(p.ref, s.ref) as the reference. On 50 million rows, the payments side must be restricted to the settlement date window before the join, in a subquery so the outer join still works, and both ref columns need the same type and collation so a hash join or index can be used. Results go into a reconciliation table that people work through and alerts watch.
Why this question is asked
Reconciliation is how a payments system proves that money it thinks it received was actually settled by the provider, and the other way round. The query itself is a textbook full outer join, but running it daily against 50 million payments exposes every practical concern: scanning history, join strategy, types that silently block indexes, NULL amounts, and what happens to the results. Interviewers like this scenario because it moves from SQL to operations. In this answer, we will write the reconciliation query, restrict it correctly to the settlement window, make the join efficient, adapt it to MySQL, and design what happens with the results.
1What We Are Looking For
A daily settlement file from the payment provider lists every payment it settled, with its reference and amount. It is loaded into settlements(ref, amount). Our payments table records every payment we believe happened. Reconciliation looks for three kinds of break: a payment we have that the provider did not settle, a settlement the provider reports that we have no payment for, and a payment both sides know about with different amounts.
A FULL OUTER JOIN on ref gives all three at once, because it keeps unmatched rows from both sides.
| Case | payments side | settlements side | Typical cause |
|---|---|---|---|
| Missing at provider | row present | NULL | failed capture, settles tomorrow |
| Missing in our system | NULL | row present | lost callback, manual refund |
| Amount mismatch | 100.00 | 99.10 | fees netted, partial capture, FX |
| Match | 100.00 | 100.00 | not reported |
2The Reconciliation Query
COALESCE(p.ref, s.ref) gives one reference column whichever side the row came from. The WHERE clause keeps only breaks: a NULL ref on either side means one side is missing, and IS DISTINCT FROM compares amounts safely even if one amount is NULL, where p.amount <> s.amount would return unknown and hide the row.
Notice where the date filter is. It sits inside the subquery on payments, not in the outer WHERE. If we wrote WHERE p.captured_at >= ... after the full join, settlement rows with no matching payment would have a NULL captured_at and be removed, so the query would never report missing payments. Filtering each input before an outer join is the safe pattern.
3Restricting the Window Without Losing Breaks
The settlements table only holds one day's file, but payments holds years of history. Without the window, the full join would treat every historical payment as missing at the provider. With it, we compare the day's payments with the day's settlements.
Real systems need a little tolerance at the edges: a payment captured at 23:59 may settle in tomorrow's file. A common approach is to compare a window slightly wider than the file's day, or to carry unmatched payments forward and re-check them against the next day's file before raising them as breaks. Either way, the window keeps the payments side to perhaps 200,000 rows out of 50 million, which an index on payments(captured_at) finds quickly.
- Load settlement fileinto settlements, typed and validated
- Select payments in the windowindex on captured_at; about 200,000 of 50M rows
- FULL OUTER JOIN on refhash join, same type and collation
- Keep breaks onlyNULL on a side, or amounts distinct
- Write to recon_breakswith run date and break type
- Alert on countsmore breaks than usual, or old open breaks
6 steps in order. Play them, or step through them one at a time.
4Making the Join Efficient
With 200,000 payments and a similar number of settlements, the planner will usually choose a hash join: it builds a hash table of one side's refs in memory and probes it with the other side, in one pass over each. That is fast as long as the join condition is a plain equality on columns of the same type.
Types are a common silent problem. If payments.ref is varchar and settlements.ref was loaded as text with trailing spaces, or as a different collation in MySQL, the comparison may need a conversion on every row, or may fail to match rows that look equal. In MySQL, comparing columns with different character sets or collations can prevent index use entirely. So the settlements loader should cast and trim refs into exactly the type and collation of payments.ref. An index on payments(ref) helps when the planner prefers a nested loop, for example when the settlement file is small.
5The MySQL Version
MySQL has no FULL OUTER JOIN, so we split the work into three queries, each simple and index-friendly, and combine them with UNION ALL.
The first two are anti-joins, rows without a partner, and the third is an inner join. The null-safe equality operator, written as less-than, equals, greater-than, plays the role of IS DISTINCT FROM when negated with NOT. Here the date filter can sit in each WHERE, because in query 1 payments is the preserved side, and in query 2 it is inside the NOT EXISTS subquery.
6What Happens With the Results
A reconciliation that prints rows to a console is forgotten by next week. We write the breaks into a table such as recon_breaks(run_date, ref, our_amount, settled_amount, break_type, status, resolved_by, resolved_at), so operations staff can work through them, mark causes and resolutions, and see which breaks are still open. Runs are idempotent: rerunning a day replaces that day's open breaks rather than duplicating them.
Alerts watch the counts and ages, not individual rows: page someone if a day has ten times the usual number of breaks, which usually means a broken file or a failed integration, or if any break stays open for more than three days. The total amount of open breaks is the number finance cares about.
7Follow-up questions
Why IS DISTINCT FROM instead of <> for amounts?
If either amount is NULL, for example a settlement line with a missing amount, <> returns unknown and WHERE drops the row, hiding a real break. IS DISTINCT FROM returns true when exactly one side is NULL and false when both are, so every difference is reported.
What if one payment is settled in several parts?
Then settlements is not one row per ref. We aggregate settlements by ref first, SUM(amount) grouped by ref, and join that to payments, otherwise a payment settled in two parts appears twice with two mismatching amounts.
How would you run this if settlements arrive for many days at once?
Load each file with its file date, run the reconciliation per settlement date, and keep each run's window aligned with its file. Processing day by day keeps each join small and makes reruns of a single day possible.
8Conclusion
In this answer, we reconciled payments with a settlement file using a FULL OUTER JOIN on ref, with COALESCE for the reference and IS DISTINCT FROM for amounts, so missing rows on either side and mismatches appear in one result. We restricted payments to the settlement window inside a subquery, so the outer join still reports settlements without payments, made sure both ref columns share type and collation for an efficient hash join, wrote the MySQL version as two anti-joins and an inner join, and stored the breaks in a table with alerts on counts and age.
Open with
I would use a FULL OUTER JOIN between payments and settlements on ref, because it keeps unmatched rows from both sides. I select COALESCE of p.ref and s.ref as the reference, both amounts, and a break type, and keep rows where p.ref is null, meaning the provider settled something we do not have, where s.ref is null, meaning we have a payment the provider did not settle, or where the amounts are distinct.
Then, if they want more
I use IS DISTINCT FROM rather than not-equals, so a NULL amount does not hide a break. With 50 million payments, the key is to restrict payments to the settlement window first, using an index on captured_at, and to do it inside a subquery before the join.
If I put that date filter in the outer WHERE, settlements without a payment would have a NULL date and be removed, so I would never see that kind of break. I would allow a little tolerance at the day boundary, or carry unmatched payments forward a day.
Then the join is a couple of hundred thousand rows on each side, which a hash join handles in one pass, as long as both ref columns have the same type and collation, so the loader should cast and trim the file's refs.
In MySQL, which has no full join, I would write two anti-joins and an inner join for mismatches, combined with UNION ALL. Results go into a reconciliation table with status and owner, and alerts fire on unusual break counts or breaks open too long.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You must reconcile a payments table in your database with a daily settlement file loaded...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/ed137801a7/