# Joins and as-of joins
source: https://docs.chalk.ai/docs/chalkdf/transformations/joins

## Match records on equal keys or the nearest observation in an ordered series.

Use an equality join to attach a user attribute to each transaction. A left join keeps a transaction even when its user has no matching record.

```python
from chalkdf import DataFrame

tx = DataFrame({"user": ["a", "a", "b"], "amount": [12, 8, 30]})

users = DataFrame({"user": ["a"], "region": ["west"]})
tx.join(users, on=["user"], how="left").select("user", "amount", "region").order_by("amount").run()
```

| user · `string` | amount · `int64` | region · `string` |
| --------------- | ---------------- | ----------------- |
| a               | 8                | west              |
| a               | 12               | west              |
| b               | 30               | null              |

User b has no region in the lookup table. Use an inner join to keep only matches. If a key matches several right-hand rows, each match contributes a result row.

### Match by time

For a rate that changes over time, use [`join_asof`](/dataframe-docs#DataFrame.join_asof) with the timestamp as `on` and the currency as `by`. With the backward strategy, a transaction takes the latest rate at or before its timestamp within that currency. Sort observations in time order and choose one rate per currency and timestamp to make ties unambiguous.

### Look up the most recent rate

Here the time key is an integer minute. A payment at minute 3 uses the rate published at minute 1. A payment at minute 5 can use the rate published that same minute.

```python
payments = DataFrame({"minute": [3, 5], "currency": ["USD", "USD"]})
rates = DataFrame({"minute": [1, 5], "currency": ["USD", "USD"], "rate": [10, 12]})
payments.join_asof(rates, on="minute", by="currency", strategy="backward").run()
```

| minute · `int64` | currency · `string` | rate · `int64` |
| ---------------- | ------------------- | -------------- |
| 3                | USD                 | 10             |
| 5                | USD                 | 12             |

A payment preceding the first rate for its currency has no match.

See [`DataFrame.join`](/dataframe-docs#DataFrame.join), [`DataFrame.join_asof`](/dataframe-docs#DataFrame.join_asof).





