The Row Pairing Model
Concepts covered: Inner Joins
A store keeps its data in separate tables. customers holds one row per shopper, and orders holds one row per purchase. An order row stores the shopper's customer_id and leaves the name where it already lives, in customers.
Splitting data this way keeps each fact in one place. If Alice changes her name, one row in customers changes and every one of her orders stays correct. The price is that a question like 'which shopper spent the most?' needs columns from both tables in the same row. A join is how a query gets them there.
Ask who placed order 103 and you do a lookup by hand. You read customer_id 2 on the order row, scan customers for the row whose customer_id is also 2, and find Bob. A join is that same lookup, run for every row at once.
Every pair of rows gets one test
The database takes one row from the first table and one row from the second, and asks a true-or-false question about the pair. For these tables the question is: does the customer's customer_id equal the order's customer_id? A pair that answers true is kept. Every other pair is thrown away. Keep this model in mind for the rest of the lesson.
With 2 customers and 3 orders there are 6 possible pairs. This table lists all of them.
| customers row | orders row | test | result |
|---|---|---|---|
| 1, Alice | 101, customer 1 | 1 = 1 | true, kept |
| 1, Alice | 102, customer 1 | 1 = 1 | true, kept |
| 1, Alice | 103, customer 2 | 1 = 2 | false, dropped |
| 2, Bob | 101, customer 1 | 2 = 1 | false, dropped |
| 2, Bob | 102, customer 1 | 2 = 1 | false, dropped |
| 2, Bob | 103, customer 2 | 2 = 2 | true, kept |
3 pairs pass, so the result has 3 rows. Each result row is one customer row and one order row placed side by side, which is how a single row can show both name and total.
The query below asks for that pairing. The line that starts with ON holds the test, and the last section of this lesson takes the rest of the syntax apart. Play it and watch each comparison come out true or false.
SELECT customers.customer_id, customers.name, orders.order_id, orders.total
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| order_id | customer_id | total |
|---|---|---|
| 101 | 1 | 50 |
| 102 | 1 | 30 |
| 103 | 2 | 75 |
What the model lets you predict
Alice appears twice in the result and Bob once. Her row passed the test with 2 different order rows, and each passing pair became its own result row.
These 3 statements answer most questions about a join before you run it. When a result has more rows than expected, some row found several partners. When a row you expected is missing, it found none.
On large tables a database skips the pair-by-pair work and uses faster matching methods. They return exactly the rows the every-pair model predicts, so the model stays a safe way to reason.
A new row arrives in orders: order 999 with customer_id 7 and total 40. The customers table still holds only Alice (1) and Bob (2). How many rows does the join return now?
- 3 rows. The new order forms 2 new pairs, and neither passes, because 7 equals neither 1 nor 2.
- 4 rows. Every row of orders appears at least once, so the new order adds one more row.
- 8 rows. The result holds every pair of 2 customers and 4 orders, whatever the test says.
- 5 rows. The new order pairs once with Alice and once with Bob, since both are customers.
Hold on to the picture of pairs and a test. The next 4 sections are all about that test: which columns it should compare, what those columns promise, how many pairs will pass, and how to write it.
About this lesson
This section is part of the Joins: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.