← Back to SQL, page 3

sql-0056 · Medium mid

How do you reconcile two lists, such as students registered in the portal and students on the exam sheet, to find who is missing from each?

A FULL OUTER JOIN on the shared key keeps rows from both lists. Rows with NULL on the right are only in the first list, rows with NULL on the left are only in the second, and rows with both sides are in both. On engines without FULL OUTER JOIN, combine a LEFT JOIN with the unmatched rows of the reverse LEFT JOIN using UNION ALL.

Full answer

Reconciliation means comparing two sources and labelling each key as matched, only in A, or only in B.

sql
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');
sql
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;
rollstatus
1portal only
2portal only
3both
5exam 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:

sql
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;
rollstatus
1portal only
2portal only
3both
5exam 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.

sql
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