1.INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL OUTER JOIN
INNER JOIN keeps only rows that match on both sides. LEFT JOIN keeps every row from the left table, filling in NULLs when there is no match on the right. RIGHT JOIN is the mirror image. FULL OUTER JOIN keeps every row from both sides, matched or not.
Key features
- INNER JOIN returns only rows with a match on both sides
- LEFT JOIN keeps every left-table row; unmatched right columns come back NULL
- RIGHT JOIN keeps every right-table row; unmatched left columns come back NULL
- FULL OUTER JOIN keeps every row from both tables, matched or not
- RIGHT JOIN is rarely needed in practice — swapping table order and using LEFT JOIN reads the same way to most authors
What happens to an unmatched row, by join type
- INNER JOIN
- customers × orders, matched only
- a customer with no orders vanishes from the result
- result size can only shrink or stay equal
- LEFT JOIN
- every customer row survives
- a customer with no orders gets one row, order columns NULL
- result size is at least the left table's row count
Command
SELECT ... FROM a INNER JOIN b ON a.key = b.key;
SELECT ... FROM a LEFT JOIN b ON a.key = b.key;
SELECT ... FROM a RIGHT JOIN b ON a.key = b.key;
SELECT ... FROM a FULL OUTER JOIN b ON a.key = b.key;INNER drops unmatched rows on both sides; LEFT keeps every left row; RIGHT keeps every right row; FULL OUTER keeps both — the join type is really just "what happens to a row with no match."