← Lessons

quiz vs the machine

Gold1360

Databases

INNER Versus OUTER Joins

Inner joins keep only matched rows while outer joins keep unmatched ones too.

5 min read · core · beat Gold to climb

Matching Rows Across Tables

A join combines rows from two tables using a condition. The kind of join decides what happens to rows that have no match on the other side.

The Join Types

  • INNER JOIN returns only rows that match on both sides. Unmatched rows are dropped.
  • LEFT OUTER JOIN keeps every row from the left table, filling missing right columns with NULL.
  • RIGHT OUTER JOIN does the same but preserves the right table.
  • FULL OUTER JOIN keeps unmatched rows from both sides.

A Common Trap

If you put a condition on the right table in the WHERE clause of a left join, NULL filled rows fail that test and disappear, quietly turning your outer join back into an inner join. To keep them, move that condition into the ON clause instead.

Finding Missing Rows

A left join where you then keep only rows whose right key is NULL is the classic way to find records on the left that have no partner on the right, such as customers with no orders.

Key idea

Inner joins return only matched pairs, outer joins also keep unmatched rows as NULL, and right table filters belong in ON to preserve them.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a LEFT OUTER JOIN do with a left row that has no match?

2. Why can a WHERE condition on the right table cancel a left join?

3. How do you find left rows with no matching right row?