SQL Join Basics and Join Types/Join on stable keys

Prefer primary keys and foreign keys over names or free-form text.

Section: Quick rules

Join on stable keys

sql
sql
Good:   orders.customer_id = customers.customer_id
Risky:  orders.customer_name = customers.name
Explanation

Keys are usually unique, indexed, and less error-prone than textual attributes.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Quick rules
Put right-side filters in ON when preserving outer rows
A `WHERE` clause can accidentally turn a left join into an inner join.
OpenIn sheetsqlsame section
INNER JOIN
Return only rows that match on both sides.
LEFT JOIN
Keep all rows from the left table and match rows from the right when available.
RIGHT JOIN
Keep all rows from the right table and match rows from the left when available.
FULL OUTER JOIN
Keep all rows from both tables and match where possible.
CROSS JOIN
Produce the Cartesian product of two tables.