Stop 06 · Joining TablesTopics · 4/10

RIGHT JOIN and FULL OUTER JOIN

Part of the SQL Roadmap

In short

When you reconcile two lists, like your sales records against the bank's settlement report, you need to see what's missing on both sides. FULL OUTER JOIN shows you all of it in one result.

RIGHT JOIN keeps all rows from the right table, and FULL OUTER JOIN keeps all rows from both. MySQL does not support FULL OUTER JOIN directly, so it is emulated with UNION.

The jaadu sits at a pharmacy counter with two long paper rolls unrolled side by side, drawing string between matching entries and pinning red flags on the ones left unpaired on each roll.

Think of it like this

Matching the bill book with the bank statement

A medical store owner in Lucknow sits with two lists: her own bill book and the bank's UPI settlement statement. Most entries match. Some bills never reached the bank, and some bank credits have no bill. Ticking off only her bill book is a LEFT (or RIGHT) join. Laying both lists side by side and marking every gap is a FULL OUTER JOIN.

Watch it happen

Keep one side, or keep both

RIGHT JOIN

  • Keeps every row of the right table
  • Left columns are NULL when unmatched
  • Same as a LEFT JOIN with the tables swapped
  • Works in PostgreSQL, MySQL and SQL Server

FULL OUTER JOIN

  • Keeps every row from both tables
  • Matched rows are joined, unmatched get NULLs
  • Perfect for reconciling two lists
  • Not in MySQL, so emulate it with UNION

Most teams rewrite RIGHT JOIN as LEFT JOIN for readability. Use FULL OUTER JOIN when gaps on both sides matter.

The big ideas

What you need to know

RIGHT is LEFT flipped

A RIGHT JOIN B is the same as B LEFT JOIN A. Reading left-to-right is easier, so most people prefer LEFT.

FULL keeps everything

Every row from both tables appears once, matched where possible, with NULLs where not.

Built for reconciliation

Comparing two systems, two months or two vendors' lists is the classic FULL OUTER JOIN job.

MySQL workaround

MySQL has no FULL OUTER JOIN. Use a LEFT JOIN, then UNION a RIGHT JOIN of the same tables.

See it in code

Walk through the code

sql
1SELECT2  COALESCE(b.txn_ref, u.txn_ref) AS txn_ref,3  b.amount AS bill_amount,4  u.amount AS bank_amount,5  CASE6    WHEN b.txn_ref IS NULL     THEN 'Missing in bill book'7    WHEN u.txn_ref IS NULL     THEN 'Missing in bank'8    WHEN b.amount <> u.amount  THEN 'Amount mismatch'9    ELSE 'Matched'10  END AS status11FROM shop_bills AS b12FULL OUTER JOIN upi_settlements AS u13  ON u.txn_ref = b.txn_ref14ORDER BY status, txn_ref;

1/3Unmatched rows have NULL on one side, so COALESCE picks whichever reference exists.

A UPI reconciliation report. In MySQL, write it as a LEFT JOIN UNION a RIGHT JOIN of the same two tables.

Save yourself the pain

Common mistakes

Oops

Mixing LEFT and RIGHT joins in one query.

Do this instead

It quickly becomes impossible to tell which rows are protected. Reorder the tables and use only LEFT JOINs.

Oops

Selecting b.txn_ref alone in a FULL OUTER JOIN.

Do this instead

It's NULL for bank-only rows. Use COALESCE(b.txn_ref, u.txn_ref) for the key.

Oops

Using UNION ALL for the MySQL workaround without care.

Do this instead

Matched rows appear in both halves. Use UNION, or add WHERE b.txn_ref IS NULL to the second half.

Your mission

Reconcile two lists

~25 min

Quick check

Table A has IDs 1, 2, 3 and table B has IDs 2, 3, 4. How many rows does A FULL OUTER JOIN B ON A.id = B.id return?

Go deeper

  1. 1Rewrite a RIGHT JOIN as a LEFT JOIN by swapping table order.
  2. 2Use FULL OUTER JOIN to compare two lists and find what is missing from each.
  3. 3Learn the MySQL workaround with LEFT JOIN UNION RIGHT JOIN.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge