liveBook · Manning

Left outer join

As explained in Learn SQL in a Month of Lunches

A left outer join is an SQL join that returns all rows from the left table together with matching rows from the right table, representing missing right-side matches with null values.

A left outer join keeps every row from the first table named in the query and adds matching data from the second table. When no match exists, right-table columns contain null values. Think of a spreadsheet lookup that preserves the complete first list, even when some entries have no match. section 9.2.1

Show every order and any promotion

SELECT oh.OrderID, p.PromotionCode FROM orderheader oh LEFT OUTER JOIN promotion p ON oh.PromotionID = p.PromotionID; Here, orderheader is the left table because it appears first. Every order appears, while orders without a promotion show null for PromotionCode. Add WHERE oh.OrderID <= 1008 to limit the orders without changing that left-join behavior. section 9.2.1

Use left joins carefully in nearby tasks:

  • Keep a filter on the right table in the ON condition when unmatched left rows must remain. A right-table condition in WHERE removes null matches and effectively produces inner-join results. section 9.2.1
  • Rewrite a right join by swapping the table order and using a left join; LEFT JOIN is the shorter equivalent of LEFT OUTER JOIN. section 9.2.4
  • Apply the pattern to orders with optional promotions, customers without orders, or customers and their qualifying orders. section 9.4
SELECT
    oh.OrderID,
    p.PromotionCode
FROM orderheader oh
LEFT OUTER JOIN promotion p
    ON oh.PromotionID = p.PromotionID;

A complete query makes the preserved table, match condition, and returned columns easy to inspect. section 9.2.1

How Learn SQL in a Month of Lunches covers it

How deep it goes
Defines preservation and nulls, demonstrates filtering traps, and notes alternatives without developing full outer joins.
Examples
Uses orders and promotions, unmatched customers, and customers’ qualifying orders as practical query exercises.
What it assumes
Assumes basic join syntax and teaches how query order identifies the left table.
What you can do after
Build it from scratch for complete-left-table reports, optional matches, unmatched records, and carefully placed filters.
How it connects
Connects JOIN, ON, WHERE, null values, inner joins, right joins, and relational database management systems.

Also explained in

Compare how 6 books explain left 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.