Full answer
Reconciliation means comparing two sources and labelling each key as matched, only in A, or only in B.
CREATE TABLE portal (roll INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE exam_sheet (roll INTEGER PRIMARY KEY, name TEXT);
INSERT INTO portal VALUES (1,'Asha'),(2,'Ben'),(3,'Chen');
INSERT INTO exam_sheet VALUES (3,'Chen'),(5,'Eli');SELECT COALESCE(p.roll, x.roll) AS roll,
CASE WHEN x.roll IS NULL THEN 'portal only'
WHEN p.roll IS NULL THEN 'exam sheet only'
ELSE 'both' END AS status
FROM portal p
FULL OUTER JOIN exam_sheet x ON x.roll = p.roll
ORDER BY roll;| roll | status |
|---|---|
| 1 | portal only |
| 2 | portal only |
| 3 | both |
| 5 | exam sheet only |
The CASE expression labels each row by which side is NULL. Roll 3 is in both lists, roll 1 and 2 are only in the portal, and roll 5 is only on the exam sheet.
On MySQL, which has no FULL OUTER JOIN, or on SQLite before 3.39, the same result comes from two LEFT JOINs:
SELECT p.roll, CASE WHEN x.roll IS NULL THEN 'portal only' ELSE 'both' END AS status
FROM portal p LEFT JOIN exam_sheet x ON x.roll = p.roll
UNION ALL
SELECT x.roll, 'exam sheet only'
FROM exam_sheet x LEFT JOIN portal p ON p.roll = x.roll
WHERE p.roll IS NULL
ORDER BY 1;| roll | status |
|---|---|
| 1 | portal only |
| 2 | portal only |
| 3 | both |
| 5 | exam sheet only |
The second half keeps only exam rows with no portal match, so matched rows are not repeated. UNION ALL is used instead of UNION because the two halves cannot overlap, and UNION would add an unnecessary duplicate-removal step.
Two details matter in real reconciliations. Normalise the key first (trim spaces, consistent case, same data type), or genuine matches will show up as "missing from both". And compare values as well as keys: p.name IS DISTINCT FROM x.name finds rows present in both lists whose details disagree.
CREATE TABLE portal (roll INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE exam_sheet (roll INTEGER PRIMARY KEY, name TEXT);
INSERT INTO portal VALUES (1,'Asha'),(2,'Ben'),(3,'Chen');
INSERT INTO exam_sheet VALUES (3,'Chen'),(5,'Eli');
SELECT COALESCE(p.roll, x.roll) AS roll,
CASE WHEN x.roll IS NULL THEN 'portal only'
WHEN p.roll IS NULL THEN 'exam sheet only'
ELSE 'both' END AS status
FROM portal p
FULL OUTER JOIN exam_sheet x ON x.roll = p.roll
ORDER BY roll;
SELECT p.roll, CASE WHEN x.roll IS NULL THEN 'portal only' ELSE 'both' END AS status
FROM portal p LEFT JOIN exam_sheet x ON x.roll = p.roll
UNION ALL
SELECT x.roll, 'exam sheet only'
FROM exam_sheet x LEFT JOIN portal p ON p.roll = x.roll
WHERE p.roll IS NULL
ORDER BY 1;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- FULL OUTER JOIN keeps every key from both sides
- Which side is NULL tells you where the key is missing
- LEFT JOIN UNION ALL anti-join emulates FULL OUTER JOIN
- Normalise keys before comparing
Common mistakes
- Using UNION instead of UNION ALL in the emulation and paying for a needless sort
- Testing a nullable non-key column to decide which side is missing
Follow-ups
- How would you also flag rows whose names differ between the two lists?
- Why is UNION ALL safe in the emulation?
Tips
- Show the label column; it turns the join into a readable report