FULL OUTER JOIN keeps every row of both tables. Rows that match are paired up; a row from either side with no partner still appears, with NULL in the other table’s columns. It combines a left join and a right join in one.
SELECT columnsFROM table1FULL OUTER JOIN table2 ON table1.column = table2.column;FULL JOIN is a shorter name for the same thing.
The examples use the Suppliers and Invoices tables from the example database.
Gaps on both sides
Section titled “Gaps on both sides”A full join is the natural way to reconcile two lists. Matching invoices against the approved supplier list shows both kinds of mismatch at once: approved suppliers who have not invoiced, and invoices from suppliers who are not approved.
SELECT Suppliers.Name AS ApprovedSupplier, Invoices.InvoiceID, Invoices.Supplier AS InvoicedByFROM SuppliersFULL OUTER JOIN Invoices ON Invoices.Supplier = Suppliers.NameORDER BY Invoices.InvoiceID;| ApprovedSupplier | InvoiceID | InvoicedBy |
|---|---|---|
| Northgate Transport | ||
| Hartwell Lighting | 1001 | Hartwell Lighting |
| Kestrel Catering | 1002 | Kestrel Catering |
| 1003 | Ashby Camera Hire | |
| Kestrel Catering | 1004 | Kestrel Catering |
- Northgate Transport is approved but has sent no invoice, so the invoice columns are empty.
- Invoice 1003 comes from Ashby Camera Hire, which is not on the approved list, so the supplier column is empty.
- Everything else matches.
An inner join would have hidden both gaps, and a left or right join would have shown only one of them.
The row with no invoice sorts first here because SQLite and SQL Server put NULLs first in an ascending sort; PostgreSQL and Oracle put them last.
Where it is not supported
Section titled “Where it is not supported”MySQL and Microsoft Access have no FULL OUTER JOIN. You can build one from a left join and a right join, joined with UNION, which also removes the second copy of each matched row, since both halves return it:
SELECT Suppliers.Name, Invoices.InvoiceIDFROM Suppliers LEFT JOIN Invoices ON Invoices.Supplier = Suppliers.NameUNIONSELECT Suppliers.Name, Invoices.InvoiceIDFROM Suppliers RIGHT JOIN Invoices ON Invoices.Supplier = Suppliers.Name;