Skip to content

Joins without surprises

Choose a join type by what you want to keep, predict the row count, and catch keys that multiply rows.

Applied ~11 min10 steps

You will be able to

  • Choose between inner, left, full, semi and anti joins
  • Predict a join's row count from its keys
  • Detect fan-out before it inflates a total

Read first

01

What does a join keep?

Read
JoinKeeps
Innerrows with a match on both sides
Leftevery left row, with blanks where nothing matches
Fullevery row from both sides
Semileft rows that have a match, adding no columns
Antileft rows with no match

Start from what you want to keep. A left join keeps all your customers. An inner join can quietly drop the ones with no orders, and they may be the ones you're studying.

02

Count the rows

Predict

Predict first

customers has three rows: Ana, Bo and Cy. orders has Ana twice and Bo once. How many rows does a left join of customers with orders have?

Make your guess between 0 and 10 rows, then reveal.

Reveal

Four: Ana twice, Bo once, and Cy once with blanks. A join makes one row per match, so a customer with five orders becomes five rows.

03

Fan-out

Read

Averaging customers.income after that join counts Ana twice. This is fan-outFan-outA join that returns extra rows because a key repeats, which silently inflates every total and average after it.: the key isn't unique on the side you assumed, and every total built on the result is wrong while looking fine.

04

Do it in the app

In the app

Two places join data.

  • In Prepare (⌘3), Join another dataset adds columns by matching keys, and supports inner, left, right, full, semi and anti. As-of join matches the nearest earlier or later time.
  • In SQL (⌘4), write the join yourself and inspect the result.

The walkthrough below starts smaller, with the average {{value}} for each {{group}}.

05

Combine datasets

In the app

Combine datasets does the same with the row counts in view. Open it above the dataset list on the Data page, or with Combine with… in a dataset's right-click menu. Choose Join ("Add columns by matching rows") or Append ("Stack rows"), and tick the datasets. Nothing changes the datasets you pick.

The Join card names each join type by what it keeps, with its row count: All here (left), Only matches (inner), All there (right) and Everything (full). Has a match and No match only filter the rows here.

A strip at the top states the grain, such as "One row per customer". Continue to analysis takes the result to the Guided page without saving a dataset first.

06

When a join repeats rows

Predict

Predict first

You join orders onto customers to study customer churn. Each customer matches several orders, so the result no longer has one row per customer. Combine offers three choices. Which keeps a fair analysis of customers?

Summarize orders per customer firstKeep the first match onlyKeep every match
Reveal

The answer: Summarize orders per customer first

Summarize first: one row per customer, with each customer's order count, total or latest order. It's the recommended choice. Keep the first match only depends on which order happens to come first. Keep every match repeats each customer's churn once per order, so counts and p-values are inflated.

07

Audit with an anti join

Explore

An anti join is the best question you can ask a key: which rows found no partner?

Run this in the SQL page on two of your own tables, with your own key, and read what comes back. Spelling differences, stray spaces and mismatched types all show up here.

SELECT a.*
FROM orders AS a
ANTI JOIN customers AS c USING (customer_id);
08

The same join in code

Read
Python
import pandas as pd

customers = pd.DataFrame({"id": [1, 2, 3], "name": ["Ana", "Bo", "Cy"]})
orders = pd.DataFrame({"id": [1, 1, 2], "total": [10, 20, 30]})

joined = customers.merge(orders, on="id", how="left", validate="one_to_many")
print(len(joined))  # 4
R
library(dplyr)

customers <- tibble(id = 1:3, name = c("Ana", "Bo", "Cy"))
orders <- tibble(id = c(1, 1, 2), total = c(10, 20, 30))

joined <- left_join(customers, orders, by = "id", relationship = "one-to-many")
nrow(joined)  # 4

Both languages let you declare the relationship you expect, and raise an error when the data break it.

09

Check your understanding

Checkpoint

Check yourself

You want a list of customers who have never ordered. Which join gives it directly?

Show the answer

Answer: c

An anti join keeps the left rows with no match, which is exactly that list.

Check yourself

After a left join, total revenue doubled. What is the most likely cause?

Show the answer

Answer: a

Fan-out repeats left-side values once per match, and sums then count them repeatedly.

Check yourself

left has ids 1, 2, 3. right has ids 1, 1, 1, 4. How many rows does a full join have?

Show the answer

Answer: 6

Id 1 matches three times (3 rows), ids 2 and 3 have no partner (2 rows), and id 4 has no left partner (1 row): 3 + 2 + 1.

Check yourself

After a join, Combine's grain strip reads "Rows repeat: 1,377 rows for 1,204 customers". What does it tell you?

Show the answer

Answer: b

The table is no longer one row per customer. Summarize the other dataset per customer first, keep one match, or treat the rows as repeated measures on purpose.

10

Recap

Recap
  • Pick the join by what you want to keep.
  • A join makes one row per match, so non-unique keys multiply rows.
  • Predict the count, compare, and audit keys with an anti join.
  • In Combine, read the grain strip, and summarize the other side first when rows repeat.

Concepts in this lesson