SQL JOIN Cheat Sheet
A practical guide to INNER, LEFT, RIGHT, FULL, CROSS, and self joins with common mistakes to avoid.
7 min read
Choose the join from the business question
INNER JOIN keeps matched rows. LEFT JOIN keeps every row from the left table and adds matches from the right. Start by deciding which records must remain even when no match exists.
SELECT
c.customer_id,
c.name,
COUNT(o.order_id) AS order_count
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name;Common join mistakes
Before aggregating, inspect the joined row count and verify the expected relationship: one-to-one, one-to-many, or many-to-many.
- Filtering the right table in WHERE after a LEFT JOIN
- Joining on a non-unique key and multiplying rows
- Counting rows instead of a nullable right-side key
- Using DISTINCT to hide an incorrect join
- Ignoring NULL when looking for unmatched records
A reliable workflow
Write each table's role in one sentence, identify the key, test the join on a small sample, and only then add aggregation. This catches most inflated-total bugs.
Turn the concept into practice
Write real queries, run them safely, and compare the result with a verified solution.
Open Practice Arena