# Query Table Tool

Query rows from a table with optional filtering, sorting, and pagination.

## When to Use

The AI uses this tool to retrieve structured data from tables for analysis, generate reports based on collected metrics, or extract specific records matching certain criteria. It supports powerful filtering and sorting to find exactly the data needed.

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `table_id` | string | Yes | The table's id or slug, as `list_tables` gives them; the slug aliases the id and either is accepted |
| `filters` | array | No | Filter conditions (see Filter Format below) |
| `search` | object | No | `{ "term", "targets"? }` text search clause. Targets are strings: column names or schema-resolved `id`; blank, nested, and unknown targets fail as unknown targets. |
| `sort` | object | No | `{ "target", "direction" }` sort clause. The target is a string column name or schema-resolved `id`; blank, nested, and unknown targets fail as unknown targets. |
| `limit` | number | No | Maximum preview rows to return (default: 25; 0..=1000). `0` returns no rows and may probe for `has_more`. |
| `offset` | number | No | Number of rows to skip (for pagination) |

### Filter Format

Each filter is an object with:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `target` | string | Yes | Column name, or `id` for the schema-resolved row identifier. Blank, nested, and unknown targets fail as unknown targets. |
| `operator` | string | Yes | Comparison operator (see below) |
| `operand` | string \| number \| boolean \| null \| array \| object | Yes | Value to compare against, in the shape the operator reads |

The operand's shape is decided by the operator. Most take one scalar. `eq` and `neq` also accept
`null`; `is_null` and `is_not_null` require it. `includes_any`, `in`, and `not_in` take a non-empty
array of scalars, and an empty one is refused rather than matching nothing. `includes` takes one
element of an array column, or an object to test an object column against. Handing a scalar operator
an array — or a set operator a scalar — is refused.

### Operators

| Operator | Description | Example |
|----------|-------------|---------|
| `eq` | Equals | `{ "target": "status", "operator": "eq", "operand": "active" }` |
| `neq` | Not equals | `{ "target": "priority", "operator": "neq", "operand": "low" }` |
| `gt` | Greater than | `{ "target": "score", "operator": "gt", "operand": 100 }` |
| `gte` | Greater than or equal | `{ "target": "age", "operator": "gte", "operand": 18 }` |
| `lt` | Less than | `{ "target": "price", "operator": "lt", "operand": 50 }` |
| `lte` | Less than or equal | `{ "target": "quantity", "operator": "lte", "operand": 10 }` |
| `contains` | Substring of text | `{ "target": "name", "operator": "contains", "operand": "John" }` |
| `is_null` | Field is null | `{ "target": "deleted_at", "operator": "is_null", "operand": null }` |
| `is_not_null` | Field is not null | `{ "target": "completed_at", "operator": "is_not_null", "operand": null }` |
| `includes` | Membership of a jsonb column | `{ "target": "tags", "operator": "includes", "operand": "urgent" }` |
| `includes_any` | Any listed element is present | `{ "target": "tags", "operator": "includes_any", "operand": ["urgent", "blocked"] }` |
| `in` | Scalar column is one of a set | `{ "target": "status", "operator": "in", "operand": ["todo", "doing"] }` |
| `not_in` | Scalar column is none of a set | `{ "target": "status", "operator": "not_in", "operand": ["done"] }` |

`contains` and `includes` are not two names for one thing, and neither works where the other does.
`contains` is a substring and applies only to a text column. `includes` is membership and applies
only to a jsonb column — an array or an object.

Against an array column, `includes` wraps a scalar operand, so `{"target": "tags", "operand":
"urgent"}` asks whether the array holds `"urgent"`. An array operand is taken as written and means
*all of*: `["red", "urgent"]` matches only a row holding both. Use `includes_any` for *any of*.
Against an object column the operand must be an object, and it matches when the column contains
every key and value given: `{"target": "meta", "operator": "includes", "operand": {"kind": "beta"}}`.

Every jsonb column is indexed for these, so membership does not read the table.

## Returns

| Field | Type | Description |
|-------|------|-------------|
| `success` | boolean | Whether the query succeeded |
| `rows` | array | Array of row objects |
| `count` | number | Number of rows returned |
| `table_id` | string | The table identifier |
| `has_more` | boolean | Whether more matching rows follow the returned page |
| `next_offset` | number | Offset for the next page; omitted when there are no more rows, including a zero-limit probe |

## Example Scenarios

### Basic Query
Retrieve all rows from a table:
```json
{ "table_id": "customers", "limit": 50 }
```

### Filtered Query
Find high-priority tasks:
```json
{
  "table_id": "tasks",
  "filters": [
    { "target": "priority", "operator": "eq", "operand": "high" },
    { "target": "status", "operator": "neq", "operand": "completed" }
  ]
}
```

### Sorted Query
Get latest orders first:
```json
{
  "table_id": "orders",
  "sort": { "target": "created_at", "direction": "desc" },
  "limit": 10
}
```

### Paginated Query
Get page 3 of results (20 per page):
```json
{
  "table_id": "products",
  "limit": 20,
  "offset": 40
}
```

## Notes

- Multiple filters are combined with AND logic (all must match)
- Filter operands preserve JSON primitive types; `null` is valid for `eq`, `neq`, `is_null`, and `is_not_null`; invalid targets, operators, and operands fail
- Sorting by invalid fields fails
- Results are returned as JSON objects with all fields
- Use `InsertTableRowTool` to add data
- Use `GetTableRowTool` to retrieve a specific row by ID
