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 roworders rowtestresult
1, Alice101, customer 11 = 1true, kept
1, Alice102, customer 11 = 1true, kept
1, Alice103, customer 21 = 2false, dropped
2, Bob101, customer 12 = 1false, dropped
2, Bob102, customer 12 = 1false, dropped
2, Bob103, customer 22 = 2true, 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
customers
customer_idname
1Alice
2Bob
orders
order_idcustomer_idtotal
101150
102130
103275

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?

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.