> For the complete documentation index, see [llms.txt](https://jacksonkasi.gitbook.io/tablecraft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jacksonkasi.gitbook.io/tablecraft/basics/2-relationships.md).

# Relationships

Data is rarely stored in a single table. You often need to fetch `Orders` with their `User`, or `Products` with their `Category`.

The TableCraft Engine makes joining tables simple and type-safe.

{% stepper %}
{% step %}

### Basic Join

To join another table, use the `.join()` method.

```typescript
// src/config/orders.ts
import { defineTable } from '@tablecraft/engine';
import { orders, users } from '../db/schema';

export const orderConfig = defineTable(orders)
  .join(users, {
    on: 'orders.user_id = users.id', // SQL-like condition
    type: 'left',                    // 'left' | 'inner' | 'right' | 'full'
    columns: ['email', 'name'],      // Columns to select from Users
  })
  .toConfig();
```

{% hint style="info" %}
When you query `orders`, the response will include `users.email` and `users.name`.
{% endhint %}
{% endstep %}

{% step %}

### Filtering by Related Data

Once joined, you can filter the main table based on the related table's columns.

```typescript
// Allow filtering orders by the user's email
export const orderConfig = defineTable(orders)
  .join(users, {
    on: 'orders.user_id = users.id',
    columns: ['email'],
  })
  .filter('users.email') // Enable filtering on this joined column
  .toConfig();
```

**API Request:** `GET /orders?filter[users.email]=jane@example.com`

This will return only the orders belonging to Jane.
{% endstep %}

{% step %}

### Sorting by Related Data

You can also sort the main list based on a column in the joined table.

```typescript
export const orderConfig = defineTable(orders)
  .join(users, {
    on: 'orders.user_id = users.id',
    columns: ['name'], // We need the name to sort by it
  })
  .sortable('users.name') // Allow sorting by user name
  .toConfig();
```

**API Request:**

* `GET /orders?sort=users.name` (A-Z by Customer Name)
* `GET /orders?sort=-users.name` (Z-A)
  {% endstep %}
  {% endstepper %}

## Advanced Join Options

{% tabs %}
{% tab title="Custom Alias" %}
If you join the same table twice (e.g., `creator` and `assignee` are both Users), use an alias.

```typescript
.join(users, {
  alias: 'creator',
  on: 'tasks.creator_id = creator.id',
  columns: ['name'],
})
.join(users, {
  alias: 'assignee',
  on: 'tasks.assignee_id = assignee.id',
  columns: ['name'],
})
```

{% endtab %}

{% tab title="Complex Conditions" %}
The `on` clause supports standard SQL strings.

```typescript
.join(subscriptions, {
  // Join only active subscriptions
  on: "users.id = subscriptions.user_id AND subscriptions.status = 'active'",
  type: 'inner'
})
```

{% endtab %}
{% endtabs %}

## Next Steps

Learn how to use [Advanced Configuration](/tablecraft/basics/3-advanced.md) like computed columns and custom transforms.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://jacksonkasi.gitbook.io/tablecraft/basics/2-relationships.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
