Cardinality
Concepts covered: Join Cardinality
Cardinality describes how many rows on one side of a relationship can match a single row on the other side. It is the difference between a join that returns the row count you planned for and one that returns 10x as many. You can work it out before running anything.
Count matches per key, multiply, then add
The pairing model gives the rule. Take one key value. If it appears on m rows of the left table and n rows of the right table, every one of those left rows passes the test with every one of those right rows. That value produces m times n result rows. Do this for each key value and add the products.
Try it on 3 customers and 4 orders. Alice has orders 101 and 102, Bob has 103 and Carol has 104.
| customer_id | Rows in customers | Rows in orders | Result rows |
|---|---|---|---|
| 1 | 1 | 2 | 1 x 2 = 2 |
| 2 | 1 | 1 | 1 x 1 = 1 |
| 3 | 1 | 1 | 1 x 1 = 1 |
| Total | 3 | 4 | 4 |
SELECT customers.name, orders.order_id
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| order_id | customer_id |
|---|---|
| 101 | 1 |
| 102 | 1 |
| 103 | 2 |
| 104 | 3 |
The result has 4 rows, the same count as orders. That is the signature of a one-to-many relationship joined on its keys. The primary key side contributes a 1 to every multiplication, so the result gets one row for each foreign key row that finds its partner.
The 3 shapes
| Shape | Each key value appears | Example | Result rows per key value |
|---|---|---|---|
| One-to-one | once on each side | a customer and their loyalty card | 1 x 1 = 1 |
| One-to-many | once on one side, many times on the other | a customer and their orders | 1 x n = n |
| Many-to-many | many times on both sides | one customer's orders and coupons | m x n |
The first 2 shapes are safe: the result never has more rows than the larger table. Many-to-many is the shape that surprises people, because both numbers in the multiplication can be bigger than 1.
When both sides repeat
The store hands out coupons, and coupons has one row per coupon with its owner's customer_id. Alice holds 2 coupons, Bob holds one and Carol holds 2. Joining orders to coupons on customer_id compares one foreign key with another, so the value 1 repeats on both sides. Predict the row count before running it: Alice 2 x 2, Bob 1 x 1, Carol 1 x 2.
SELECT
orders.order_id,
orders.customer_id,
coupons.code
FROM orders
INNER JOIN coupons
ON orders.customer_id = coupons.customer_id| order_id | customer_id | total |
|---|---|---|
| 101 | 1 | 50 |
| 102 | 1 | 30 |
| 103 | 2 | 75 |
| 104 | 3 | 20 |
| coupon_id | customer_id | code |
|---|---|---|
| 501 | 1 | SAVE5 |
| 502 | 1 | SHIP0 |
| 503 | 2 | SAVE5 |
| 504 | 3 | TEA10 |
| 505 | 3 | SHIP0 |
| order_id | customer_id | code |
|---|---|---|
| 101 | 1 | SAVE5 |
| 101 | 1 | SHIP0 |
| 102 | 1 | SAVE5 |
| 102 | 1 | SHIP0 |
| 103 | 2 | SAVE5 |
| 104 | 3 | SHIP0 |
| 104 | 3 | TEA10 |
7 rows come back from a table of 4 orders and a table of 5 coupons. Each of Alice's 2 orders sits beside each of her 2 coupons, which makes 4 rows about a shopper who ordered twice. No single row is false. The trouble starts when you add things up.
| order_id | customer_id | total |
|---|---|---|
| 101 | 1 | 50 |
| 102 | 1 | 30 |
| 103 | 2 | 75 |
| 104 | 3 | 20 |
| coupon_id | customer_id | code |
|---|---|---|
| 501 | 1 | SAVE5 |
| 502 | 1 | SHIP0 |
| 503 | 2 | SAVE5 |
| 504 | 3 | TEA10 |
| 505 | 3 | SHIP0 |
Orders alone
Adds total across the 4 rows of orders.
SELECT
SUM(total) AS revenue
FROM orders| revenue |
|---|
| 175 |
After join
Adds total across the 7 joined rows, where 3 orders appear twice.
SELECT
SUM(orders.total) AS revenue
FROM orders
INNER JOIN coupons
ON orders.customer_id = coupons.customer_id| revenue |
|---|
| 275 |
Revenue was 175 and the joined query reports 275. Orders 101, 102 and 104 each appear twice, so their totals are added twice. This is called fan-out. It is costly because the query runs cleanly and the inflated number looks plausible.
Check the keys before you join
You can see fan-out coming. Group each table by the join key and keep the keys that appear more than once. If neither table returns rows, the join is one-to-one. If only one does, it is one-to-many. If both do, it's many-to-many and the result will multiply.
SELECT
customer_id,
COUNT(*) AS coupon_count
FROM coupons
GROUP BY customer_id
HAVING COUNT(*) > 1| coupon_id | customer_id | code |
|---|---|---|
| 501 | 1 | SAVE5 |
| 502 | 1 | SHIP0 |
| 503 | 2 | SAVE5 |
| 504 | 3 | TEA10 |
| 505 | 3 | SHIP0 |
| customer_id | coupon_count |
|---|---|
| 1 | 2 |
| 3 | 2 |
coupons repeats customer_id 1 and 3. Run the same check on orders and it returns customer_id 1. Both tables repeat a key, so the multiplication was predictable from 2 short queries.
A teams table has 4 rows with city values A, A, B, C. A players table has 5 rows with city values A, B, B, B, D. How many rows does a join on city return?
- 9 rows: the 4 rows of teams plus the 5 rows of players.
- 5 rows: A gives 2 x 1, B gives 1 x 3, and C and D give 0.
- 20 rows: each of the 4 teams rows pairs with all 5 players rows.
- 2 rows: one for A and one for B, the 2 values both tables share.
Make the prediction a habit. Before running a join, write down the row count you expect. After running it, compare. A result larger than the foreign key side's table means some key repeated where you assumed it was unique.
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.