Practical guide

SQL JOINs for Beginners: Match Rows Across Tables

A JOIN combines related rows using a condition, commonly a primary key matched to a foreign key. Check key uniqueness before trusting totals.

What to do

  1. Join orders to customers with SELECT o.id, c.name FROM orders AS o JOIN customers AS c ON o.customer_id = c.id;. JOIN without a qualifier means INNER JOIN in many databases.
  2. Use LEFT JOIN when you need every row from the left table even if no related row exists in the right table.
  3. Inspect unmatched rows by filtering for a NULL value from the right-side table after a LEFT JOIN.
  4. Count rows before and after joining. If a key appears multiple times on both sides, the result can multiply rows.

Checks and limitations

  • A missing or incomplete ON condition can create a very large Cartesian result.
  • Do not assume IDs are unique in imported data; validate the join keys and resulting row counts.