liveBook · Manning

Outer join

As explained in Learn SQL in a Month of Lunches

A full outer join returns matching rows and preserves unmatched rows from both joined inputs, filling missing columns with null values.

An outer join keeps rows that do not have a matching value in the other table. A full outer join keeps unmatched rows from both tables, filling missing columns with null values. section 9.2 section 9.2.4

Keep every promotion and every order

SELECT p.PromotionCode, oh.OrderID FROM promotion p FULL OUTER JOIN orderheader oh ON p.PromotionID = oh.PromotionID; A promotion without an order appears with a null OrderID; an order without a matching promotion appears with a null PromotionCode. Matching rows contain values from both tables. section 9.2.4

Choose the join type according to which unmatched rows matter:

  • Use LEFT JOIN to preserve every row from the left table. section 9.2.4
  • Use RIGHT JOIN to preserve every row from the right table. section 9.2.3
  • Use FULL OUTER JOIN when both tables must contribute unmatched rows. section 9.2.4

FULL JOIN may be accepted as a shorter spelling, but support varies by database system. MySQL, MariaDB, and SQLite do not support this join type here. section 9.2.4

How Learn SQL in a Month of Lunches covers it

How deep it goes
Defines the result and portability limits, but defers a full query walkthrough and alternative construction.
Examples
Uses promotion and orderheader tables to show matching rows and unmatched rows containing nulls.
What it assumes
Assumes familiarity with inner joins, JOIN, ON, and matching key columns.
What you can do after
Apply it via SQL to combine two tables while retaining unmatched rows from both inputs.
How it connects
Connects full outer join with left outer join, right outer join, inner join, JOIN, and null values.

Also explained in

Compare how 2 books explain outer join →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.