Merge and Concatenate
The merge statement joins two tables. The active table is the left table; the named table is the right table.
Basic merge (inner join)
Keeps only rows where customer_id exists in both tables.
Join types
Prefix the merge with the join type:
with sales as enriched
left merge customers on customer_id
with sales as enriched
right merge customers on customer_id
with sales as enriched
inner merge customers on customer_id
with sales as enriched
outer merge customers on customer_id
| Type | Behaviour |
|---|---|
| (none) | Inner join — only matching rows |
left |
All rows from left, matched rows from right |
right |
All rows from right, matched rows from left |
inner |
Same as default — only matching rows |
outer |
All rows from both tables |
Join keys
Single key
Multiple keys
Different key names
When the join columns have different names in each table:
Handling duplicate column names
When both tables have columns with the same name (other than the join key), suffixes are added automatically. Customise them:
Example: enriching a fact table
load "orders.csv" as orders
load "customers.csv" as customers
load "products.csv" as products
with orders as enriched
left merge customers on customer_id
left merge products on product_id
filter status == "complete"
select order_id, date, customer_name, product_name, amount
concat — stack tables vertically
Append rows from another table onto the active table:
Both tables must have compatible columns. Extra columns in either table will be filled with null for the rows where they are absent.
intersect — keep only common rows
Keep rows that appear in both the active table and the named table (set intersection):
Duplicate rows are removed from the result.
exclude — remove rows present in another table
Remove rows from the active table that appear in the named table (set difference):
Duplicate rows are removed before the comparison.