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_idRows in customersRows in ordersResult rows
1121 x 2 = 2
2111 x 1 = 1
3111 x 1 = 1
Total344
SELECT customers.name, orders.order_id
FROM customers
INNER JOIN orders
  ON customers.customer_id = orders.customer_id
customers
customer_idname
1Alice
2Bob
3Carol
orders
order_idcustomer_id
1011
1021
1032
1043

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

ShapeEach key value appearsExampleResult rows per key value
One-to-oneonce on each sidea customer and their loyalty card1 x 1 = 1
One-to-manyonce on one side, many times on the othera customer and their orders1 x n = n
Many-to-manymany times on both sidesone customer's orders and couponsm 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
orders
order_idcustomer_idtotal
101150
102130
103275
104320
coupons
coupon_idcustomer_idcode
5011SAVE5
5021SHIP0
5032SAVE5
5043TEA10
5053SHIP0
Result
order_idcustomer_idcode
1011SAVE5
1011SHIP0
1021SAVE5
1021SHIP0
1032SAVE5
1043SHIP0
1043TEA10

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.

orders
order_idcustomer_idtotal
101150
102130
103275
104320
coupons
coupon_idcustomer_idcode
5011SAVE5
5021SHIP0
5032SAVE5
5043TEA10
5053SHIP0

Orders alone

Adds total across the 4 rows of orders.

SELECT
  SUM(total) AS revenue
FROM orders
Result
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
Result
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
coupons
coupon_idcustomer_idcode
5011SAVE5
5021SHIP0
5032SAVE5
5043TEA10
5053SHIP0
Result
customer_idcoupon_count
12
32

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?

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.