SELECT c.customer_id, c.name, o.order_id
FROM customers c
INNER JOIN orders o ON o.customer_id = c.customer_id;Use `INNER JOIN` when you only want records with matching keys in both tables.
Core SQL join types with practical examples, NULL behavior, and quick rules for correct join selection.
The essential join shapes every developer should know.
SELECT c.customer_id, c.name, o.order_id
FROM customers c
INNER JOIN orders o ON o.customer_id = c.customer_id;Use `INNER JOIN` when you only want records with matching keys in both tables.
SELECT c.customer_id, c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;Rows from `customers` without a matching order still appear, with right-side columns as `NULL`.
SELECT c.customer_id, c.name, o.order_id
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id;Many teams prefer rewriting this as a `LEFT JOIN` by swapping table order for readability.
SELECT c.customer_id, c.name, o.order_id
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.customer_id;Useful for reconciliation reports and data comparison. Some databases require emulation instead of native support.
SELECT s.size, c.color
FROM sizes s
CROSS JOIN colors c;Use carefully. Output size becomes `left_rows × right_rows`.
Fast mental models that prevent common mistakes.
Good: orders.customer_id = customers.customer_id
Risky: orders.customer_name = customers.nameKeys are usually unique, indexed, and less error-prone than textual attributes.
A `WHERE` clause can accidentally turn a left join into an inner join.
-- Keep all customers, only match paid orders
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid';When using outer joins, filters on the optional side often belong inside the `ON` clause.