TExcelTAD
Home › SQL › SQL FULL JOIN

SQL FULL JOIN

FULL JOIN (also FULL OUTER JOIN) returns all rows from both tables. Matching rows are combined; rows without a partner on either side are kept, with NULLs for the missing side. It is like a LEFT and a RIGHT join at the same time.

sql
SELECT columns
FROM table1
FULL JOIN table2 ON table1.column = table2.column;

Countries of customers and suppliers

Which countries have customers, which have suppliers, and which have both? Join the two lists of countries:

sql · try it yourself
SELECT cc.country AS customer_country,
       sc.country AS supplier_country
FROM (SELECT DISTINCT country FROM customers) AS cc
FULL JOIN (SELECT DISTINCT country FROM suppliers) AS sc
  ON sc.country = cc.country
ORDER BY COALESCE(cc.country, sc.country);
customer_country | supplier_country
-----------------+-----------------
Australia        | NULL
Austria          | NULL
Canada           | NULL
France           | France
Germany          | NULL
India            | India
Ireland          | NULL
Italy            | NULL
Japan            | Japan
Mexico           | NULL
Nigeria          | NULL
Norway           | Norway
Portugal         | NULL
South Korea      | NULL
Spain            | Spain
Sweden           | NULL
NULL             | Switzerland
Turkey           | NULL
UK               | UK
USA              | USA
Vietnam          | NULL
(21 rows)

Rows with both columns filled are countries with customers and suppliers. Switzerland only has a supplier; most countries only have customers.

Labelling the matches

COALESCE picks the non-NULL key, and a CASE tells you where the row came from. Wrapped in a subquery, we can then count each kind:

sql · try it yourself
SELECT presence, COUNT(*) AS countries, GROUP_CONCAT(country, ', ') AS list
FROM (
  SELECT COALESCE(cc.country, sc.country) AS country,
         CASE
           WHEN cc.country IS NULL THEN 'supplier only'
           WHEN sc.country IS NULL THEN 'customers only'
           ELSE 'both'
         END AS presence
  FROM (SELECT DISTINCT country FROM customers) AS cc
  FULL JOIN (SELECT DISTINCT country FROM suppliers) AS sc
    ON sc.country = cc.country
  ORDER BY country
)
GROUP BY presence
ORDER BY presence;
presence       | countries | list
---------------+-----------+---------------------------------------------------------------------------------------------------------------------
both           | 7         | France, India, Japan, Norway, Spain, UK, USA
customers only | 13        | Australia, Austria, Canada, Germany, Ireland, Italy, Mexico, Nigeria, Portugal, South Korea, Sweden, Turkey, Vietnam
supplier only  | 1         | Switzerland
(3 rows)

Comparing two lists

FULL JOIN is perfect for reconciling two versions of data. Imagine a price list from a supplier that we compare with our catalogue:

sql · try it yourself
CREATE TABLE new_prices (product TEXT, price REAL);
INSERT INTO new_prices VALUES
  ('Green Tea', 9.25), ('Chai Latte Mix', 11.0), ('Matcha Powder', 14.9);

SELECT p.name AS ours, n.product AS theirs, p.price AS old_price, n.price AS new_price
FROM (SELECT name, price FROM products WHERE supplier_id = 1) AS p
FULL JOIN new_prices AS n ON n.product = p.name
ORDER BY COALESCE(p.name, n.product);
3 rows affected.

ours           | theirs         | old_price | new_price
---------------+----------------+-----------+----------
Chai Latte Mix | Chai Latte Mix | 11        | 11
Espresso Beans | NULL           | 24.5      | NULL
Green Tea      | Green Tea      | 8.75      | 9.25
NULL           | Matcha Powder  | NULL      | 14.9
(4 rows)

At a glance: Espresso Beans is missing from the new list, Matcha Powder is new, Green Tea got more expensive and Chai Latte Mix is unchanged.

FULL JOIN without FULL JOIN

MySQL has no FULL JOIN, and neither did SQLite before 3.39. The usual replacement is a LEFT JOIN plus the unmatched rows of a reversed LEFT JOIN, glued together with UNION ALL:

sql
SELECT a.x, b.x FROM a LEFT JOIN b ON b.x = a.x
UNION ALL
SELECT a.x, b.x FROM b LEFT JOIN a ON a.x = b.x WHERE a.x IS NULL;
Database FULL JOIN
PostgreSQL, SQL Server, Oracle yes
SQLite since 3.39
MySQL / MariaDB no — emulate with UNION

Test yourself

Which rows does a FULL JOIN return?

  1. Only matches
  2. All left rows
  3. All right rows
  4. All rows from both tables, matched where possible

How do you emulate FULL JOIN in MySQL?

  1. CROSS JOIN
  2. LEFT JOIN … UNION ALL the unmatched rows of the reverse LEFT JOIN
  3. OUTER JOIN
  4. NATURAL JOIN