beginnerdbms

Joins

How you stitch two tables together based on a shared column.

Key Points

What it is

A JOIN combines rows from two tables by matching values in a specified column. The type of join determines what happens when a row in one table has no match in the other. INNER, LEFT, RIGHT, and FULL OUTER are the four core types.

Why it exists

Data is split across tables to avoid repetition. Joins are how you bring it back together at query time without having to store it redundantly.

Intuition

You have a table of customers and a table of orders. Not every customer has placed an order. If you want to see all customers and their orders, use a LEFT JOIN on customers. Customers with no orders still show up, but the order columns are NULL. If you want only customers who've actually ordered, use INNER JOIN and the non-buyers disappear from the result. The mental model: LEFT JOIN is 'give me everything on the left, attach whatever matches from the right'. INNER JOIN is 'only give me the rows where both sides agreed to show up'.

Example

Customers table: (1, Alice), (2, Bob), (3, Carol). Orders table: (order 101, customer 1), (order 102, customer 1), (order 103, customer 2). INNER JOIN result: Alice + order 101, Alice + order 102, Bob + order 103. Carol is excluded. LEFT JOIN result: Alice + order 101, Alice + order 102, Bob + order 103, Carol + NULL.

Carol shows up in LEFT JOIN because she's on the left (customers) side, even though she has no orders. INNER JOIN drops her entirely.

When to use

Common mistakes

Notes

In practice

Joins are one of the most used operations in any SQL-based analysis. Getting join types wrong is one of the most common sources of incorrect results in data pipelines — a silent INNER JOIN dropping rows can skew your entire analysis without any error message.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

relational-modelsql-basicsnormalization
Chirp at me!