Joins and NULLs

Combine tables and handle missing matches correctly.

Notes

INNER JOIN keeps only matches. LEFT JOIN keeps every row from the left table and fills NULL when there is no match.

A condition in WHERE after a LEFT JOIN can accidentally remove the unmatched rows.

Key points

  • Choose the join by which rows you must keep.
  • Test NULL with IS NULL.
  • A table can be joined to itself with aliases.