What is the difference between INNER JOIN and LEFT JOIN
· Category: SQL & Databases
Short answer
INNER JOIN returns only rows with matching keys in both tables. LEFT JOIN returns all rows from the left table and matched rows from the right, filling with NULL where there is no match.
Key differences
- Result set: INNER JOIN excludes non-matching rows from both sides. LEFT JOIN preserves every left table row.
- Nulls: LEFT JOIN produces
NULLfor right-table columns when no match exists. - Use case: Use INNER JOIN when you only care about existing relationships. Use LEFT JOIN when you need all primary records plus optional related data.
When to use each
- Use INNER JOIN for core reports where unmatched rows are irrelevant.
- Use LEFT JOIN for lists that must include all items even if some attributes are missing, such as all customers including those without orders.r