Back to Join (,) and Union Join (uj)

Joins · Join (,) and Union Join (uj) · Practice Question

Join Two Tables with Shared Columns

Sign in to track your progress

Question

Given the two tables:

a:([] names:`Tom`Mary`Jack; country:`Ireland`England`France)
b:([] names:`Sean`Anne`Roy; country:`England`Germany`Scotland)

Join both tables to create a single table, t, containing all records.

Expected result:

names country
--------------
Tom   Ireland
Mary  England
Jack  France
Sean  England
Anne  Germany
Roy   Scotland

Solution

t:a,b

Alternative Solution

t:a uj b

Alternative Solution

t:(uj/) (a;b)

Explanation

Both a and b have the exact same columns (names and country), so plain join (,) works directly — it stacks b's rows underneath a's, requiring the schemas to line up exactly.

uj (union join) is built for a more general case: tables whose columns don't fully match — any column missing from one table is added to the result and filled with nulls for that table's rows. Since a and b happen to share every column here, a uj b produces the same result as a,b, but it would keep working even if, say, b had an extra column that a didn't.

(uj/) (a;b) reaches the same result with the over iterator (/), folding uj across a list of tables — useful when there are more than two tables to combine and the count isn't known in advance.

Both tables passed to uj must be the same kind: either both unkeyed or both keyed. Mixing a keyed and an unkeyed table signals an error ('type or 'nyi, depending on which side is keyed).

a upsert b is a related but distinct operation worth contrasting: on unkeyed tables it behaves like , and simply appends b's rows, but on keyed tables it would update any row in a whose key already exists in b, inserting only the genuinely new keys — a join, plus an update, in one step.