← Lessons

quiz vs the machine

Silver1080

Databases

Inner vs Outer Joins

Inner joins keep only matching rows while outer joins preserve unmatched rows from one or both sides with nulls.

4 min read · intro · beat Silver to climb

What A Join Does

A join combines rows from two tables based on a matching condition, usually a shared key. The join type decides what happens to rows that have no match.

Inner Join

An inner join returns only rows where the condition matches on both sides. Customers with no orders simply disappear from the result. It is the strictest and most common join.

Outer Joins

Outer joins keep unmatched rows and fill the missing columns with null:

  • LEFT JOIN keeps every row from the left table.
  • RIGHT JOIN keeps every row from the right table.
  • FULL JOIN keeps unmatched rows from both tables.

A Common Mistake

Putting a condition on the right table inside WHERE after a LEFT JOIN turns it back into an inner join, because null fails the comparison. Put that filter in the ON clause instead.

Key idea

Inner joins keep only matched rows, while left, right, and full outer joins preserve unmatched rows from one or both tables using null.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens to unmatched rows in an inner join?

2. A LEFT JOIN preserves which rows?

3. Why can a WHERE filter on the right table break a LEFT JOIN?