Matching on a Key

The idea is simple and the failures are not. Build a lookup from one dataset keyed by the shared identifier, then walk the other and match.

Always count what did not match. A join that silently drops rows is the classic invisible error in data work: the result looks entirely reasonable and is missing a third of the population.

Four reasons a join loses rows.

Types differ. The number 42 and the string "42" do not match.

Whitespace or case differ on a text key.

A leading zero was lost when an identifier passed through a spreadsheet.

You joined on a name rather than an id, and names differ by spelling, punctuation and accents.

When a join loses rows, print a handful from each side and compare them character by character. The answer is almost always visible immediately and almost never what you assumed.

✦ Duplicate keys multiply rows

If the lookup side has two rows with the same key, every matching row on the other side becomes two rows. Counts then rise for no visible reason, and the total no longer means what you think. Check both sides for duplicate keys before joining, not after the numbers look odd.

◈ Graded Challenge

Write join(main, lookup) combining two lists of records on the key "id". Build the lookup from the second list. For each record in main, find the matching lookup record and merge it in, with the lookup's fields added to the main record. Keys must be compared as text, so the number 42 and the string "42" are the same key. Return an array of two items: the list of merged records, and the number of main records that found no match.

Name it exactly: join(main, lookup)

3 visible + 5 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Grouping and Summarizing →
← Back