Start here
A join puts rows from two tables side by side, using a key.
Real data lives in many tables. A customer is in one table. Their orders are in another table.
A join puts rows from two tables side by side. It uses a column that both tables share. One result then answers a question about both tables.
In chapter 1 you learned NULL and grain. NULL means a value is missing. Grain is what one row stands for.
The shop again
This chapter uses the same shop as chapter 1. Here is all the data. Every result in this chapter comes from these rows.
| customer_id | name | country |
|---|---|---|
| 1 | Asha | IN |
| 2 | Ben | GB |
| 3 | Carla | US |
| 4 | Dev | NULL |
| 5 | Omar | IN |
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| order_id | product | quantity |
|---|---|---|
| 101 | Pen | 2 |
| 101 | Notebook | 1 |
| 102 | Bag | 1 |
| 103 | Lamp | 1 |
| 104 | Pen | 3 |
| 106 | Chair | 1 |
| 106 | Lamp | 1 |
Two facts matter most in this chapter.
- Omar is a customer, but he has no orders.
- Order 105 exists, but it has no items.
What you will learn
- Say how two tables relate: one-to-many and more.
- Choose between INNER JOIN, LEFT JOIN and FULL JOIN.
- Predict how many rows a join returns.
- Find two common bugs: sums that double, and rows that vanish.
- Find rows with no match: EXISTS, NOT EXISTS and NOT IN.
- Check a join with counts.
Joins are a very common interview topic. The examples use PostgreSQL. When another database differs, we tell you.
Before you run a join, guess how many rows it returns. Then run it and compare. A wrong guess teaches you the most.
