Loading...
Loading...
Visual reference for the SQL joins everyone confuses, plus a sample query and use case for each.
Only rows in both tables
Returns rows where the key matches in both tables. Most common join.
SELECT u.name, o.total
FROM users u
INNER JOIN orders o
ON o.user_id = u.id;Customers who placed orders.
All from left, matched from right
All rows from the left table; right side is NULL when no match.
SELECT u.name, o.total
FROM users u
LEFT JOIN orders o
ON o.user_id = u.id;All users; show their orders if any.
All from right, matched from left
All rows from the right table; left side is NULL when no match.
SELECT u.name, o.total
FROM users u
RIGHT JOIN orders o
ON o.user_id = u.id;Rare. Same effect as flipping LEFT JOIN.
Everything from both sides
All rows from both tables; NULL on the side with no match.
SELECT u.name, o.total
FROM users u
FULL OUTER JOIN orders o
ON o.user_id = u.id;Reconciliation: find rows missing on either side.
Cartesian product of both sides
Every left row paired with every right row. Use deliberately.
SELECT a.day, b.product
FROM days a
CROSS JOIN products b;Generate calendar product matrices.
Joining a table to itself
Used for hierarchies and same-table comparisons. Just an INNER/LEFT JOIN with aliases.
SELECT e.name, m.name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.id;Employee manager, comments parent.
We turn AI cheatsheets into production code. Tell us what you're building.