Back to Left-Join (lj), Inner-Join (ij), Plus-Join (pj)

Joins · Left-Join (lj), Inner-Join (ij), Plus-Join (pj) · Practice Question

Add Columns from a Keyed Table with Left-Join

Sign in to track your progress

Question

Given the table:

t:([] names:`Tom`Mary`Jack`Sean`Anne`Roy;
      country:`Ireland`England`France`England`Germany`Scotland;
      age:10 13 14 18 21 41;
      employed:101101b)

Join the following country-code information to t:

codes:([] Country:`Ireland`England`France`Germany`Scotland;
          digits:("+353";"+44";"+33";"+49";"+44"))

Expected result:

names country  age employed digits
----------------------------------
Tom   Ireland  10  1        "+353"
Mary  England  13  0        "+44"
Jack  France   14  1        "+33"
Sean  England  18  1        "+44"
Anne  Germany  21  0        "+49"
Roy   Scotland 41  1        "+44"

Solution

t:t lj 1!`country xcol codes

Explanation

lj (left-join) takes an unkeyed (or keyed) table on the left and a keyed table on the right. For each row of the left table, it looks for a matching row in the right table using the right table's key column(s), and appends or updates that row's columns from the match.

codes isn't keyed yet, and its key column needs to be named country (lowercase) to match t's column, not Country. `country xcol codes renames the column, and 1! keys the result on its first column — turning codes into a table keyed on country, ready to be joined against.

The result always has the same number of rows as the left table (t) — a left join never adds or removes rows, it only enriches existing ones. Every country in t finds a match in codes here, so every row picks up a digits value.