sqlcmd.net validated sql reference
advanced joining SQL Server PostgreSQL SQLite

Combine All Rows From Both Tables With FULL OUTER JOIN

Return all rows from both tables, filling NULLs on either side when there is no matching row.

Docker-validated Not currently validation-green

Find all employees and all departments, showing gaps on both sides

Ada matches Engineering. Bob has no department (dept_id is NULL), so department is NULL. HR has no employees, so employee is NULL. COALESCE in ORDER BY sorts NULL employees and departments last.

Rows loaded before the example query runs.
Setup
CREATE TABLE departments (id INT, name VARCHAR(50));

CREATE TABLE employees (id INT, name VARCHAR(50), dept_id INT);

INSERT INTO
  departments (id, name)
VALUES
  (1, 'Engineering'),
  (2, 'HR');

INSERT INTO
  employees (id, name, dept_id)
VALUES
  (1, 'Ada', 1),
  (2, 'Bob', NULL);
Shared across supported engines.
SQL
SELECT
  e.name AS employee,
  d.name AS department
FROM
  employees e
  FULL OUTER JOIN departments d ON e.dept_id = d.id
ORDER BY
  COALESCE(e.name, 'zzz'),
  COALESCE(d.name, 'zzz');
Returned rows for the shared example.
employeedepartment
AdaEngineering
BobNULL
NULLHR

MySQL and MariaDB do not support FULL OUTER JOIN natively. The emulation uses LEFT JOIN UNION RIGHT JOIN to produce the same result. SQL Server and PostgreSQL use identical native syntax.

Where this command helps.

  • reconciling two datasets to find gaps on either side
  • producing audit views of matched and unmatched records together

What the command is doing.

FULL OUTER JOIN combines the behavior of LEFT JOIN and RIGHT JOIN. Every row from both tables appears in the result. Where no match exists, columns from the other table are NULL. This is useful for finding gaps in both directions at once, such as unassigned employees and unfilled positions simultaneously.