Joins

Combine matching rows from two or more tables into one result, using the foreign keys that link them.

What is it?

You split data across tables on purpose: users and orders, linked by a user_id foreign key. But some questions span both, like "show each order with the name of the person who placed it." Neither table has both pieces.

A join answers that. It combines rows from two tables on a matching column (usually a foreign key matching a primary key), as if the tables were glued side by side wherever they match.

The two most common kinds:

- INNER JOIN returns only rows with a match in both tables. - LEFT JOIN returns every row from the left (first) table, with empty values where the right table has no match.

Explain like I'm 10

Picture two spreadsheets: employees with a department ID, and department IDs with names. A join is a coworker who hands you one combined sheet where each employee row also shows the department name, matched by that shared ID.

Examples

INNER JOIN — only matching rows

SELECT orders.id, orders.total_cents, users.name
FROM orders
INNER JOIN users ON orders.user_id = users.id;

Returns one row per order, but only for orders whose user_id matches a real user. An order with a broken or missing user_id is left out.

LEFT JOIN — keep unmatched rows too

SELECT users.name, orders.id AS order_id
FROM users
LEFT JOIN orders ON orders.user_id = users.id;

Returns every user, even those with no orders. For them, order_id comes back as empty (NULL) instead of the row disappearing.

How it works

For each row in the first table, the database finds rows in the second table where the ON ... condition is true, and outputs one combined row per match.

- INNER JOIN: a first-table row with no matches adds nothing. - LEFT JOIN: a left-table row with no matches still appears once, with the second table's columns empty.

The database often uses an index on the joined column to find matches fast, instead of comparing every row with every other row.

Why does it exist?

Normalization splits data into separate, non-duplicated tables. That would be nearly useless if you couldn't bring related pieces back together for one question. Joins make normalized design practical to query.

When to use it

Use a join when the answer needs data from two or more related tables: an order with the customer's name, a blog post with its author, a product with its category. Use LEFT JOIN to keep first-table rows that have no match (like all users, including those with zero orders).

When not to use it

If you only need data from one table, a join just adds complexity and cost. Joining very large tables without a supporting index can be slow; sometimes it's better to add an index or rethink the schema.

Common mistakes

  • Forgetting the ON condition, which creates a 'cross join': every row of the first table paired with every row of the second, a combinatorial explosion.

  • Using INNER JOIN when you wanted to keep unmatched rows, silently losing data (like customers with no orders vanishing).

  • Not qualifying column names (id instead of orders.id) when both tables share a column name, causing ambiguity errors.

Practice exercises

  1. Easy:

    Write an INNER JOIN between products and categories (products has a category_id foreign key) to list each product with its category name.

  2. Medium:

    Write a LEFT JOIN listing every category with its products, including categories with zero products.

  3. Hard:

    Explain what you'd get from joining two tables with an always-true ON condition (e.g. ON 1 = 1), and why that's usually a mistake.

Interview questions

What's the difference between an INNER JOIN and a LEFT JOIN?

INNER JOIN returns only rows with a match in both tables. LEFT JOIN returns every row from the left table, with empty values where the right side has no match.

Why are joins necessary if data is split across normalized tables?

Normalization deliberately avoids duplicating data across tables. Joins recombine that related data when a query needs both.

What happens if you write a join without an ON condition?

You get a cross join: every row of the first table paired with every row of the second. That's almost never what you want.