---
title: "Queries"
description: "Saved table operations agents, functions, and widgets can call."
canonical_url: "https://docs.vettero.com/build/queries"
---
# Queries

> Saved table operations agents, functions, and widgets can call.

**Queries** are saved [**table**](https://docs.vettero.com/data/tables) operations in your workspace. You pick a type (select, insert, update, delete, archive, or aggregation), target a table, configure filters and values in the visual builder, and optionally declare inputs so callers pass validated arguments.

Use them to encapsulate table reads and writes behind a clear name — then attach the same operation as an agent tool, MCP tool, function resource, or widget resource. Queries are **not** raw SQL; the builder compiles your filters against table rows.

## When to use

- Reuse the same table read or write from [agents](https://docs.vettero.com/build/agents), [functions](https://docs.vettero.com/build/functions), [widgets](https://docs.vettero.com/build/widgets), and [workflows](https://docs.vettero.com/build/workflows)
- Keep filters and field mappings in one place instead of duplicating workflow table blocks
- Expose a typed tool surface with an input schema and a server-generated result schema

Prefer inline [table blocks](https://docs.vettero.com/build/workflows/blocks/table) only for one-off ops that will not be reused.

## Create a query

Open **Build → Queries** for a workspace-wide query, or open an agent’s builder and use the **Queries** panel for one scoped to that agent.

1. Click **Create Query** (or **Build With AI** to draft it in [Studio](https://docs.vettero.com/build/studio)).
2. Enter a **Name**, **Type**, **Table**, and optional **Description**.
3. Click **Create** — Vettero opens the query detail page.

**Type** and **Table** are fixed after create. The **Result schema** is generated on the server when you save — you do not edit it by hand.

## Open the query builder

From the query detail page, click **Edit** to open the **Query Builder**. Use **Save** when the config is valid.

<table>
<thead>
  <tr>
    <th>
      Section
    </th>
    
    <th>
      What you configure
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Name
      </strong>
      
       / <strong>
        Description
      </strong>
    </td>
    
    <td>
      Identity; the description becomes agent/MCP tool help text
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Input schema
      </strong>
    </td>
    
    <td>
      Arguments callers pass in (bound as <code>
        args.<name>
      </code>
      
       in filters and values)
    </td>
  </tr>
  
  <tr>
    <td>
      Type editor
    </td>
    
    <td>
      Filters, values, pagination, and sorting for the chosen operation
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Result schema
      </strong>
    </td>
    
    <td>
      Read-only; regenerated on save from type, config, and table fields
    </td>
  </tr>
</tbody>
</table>

Anywhere a filter or field value can be set, pick a **fixed value** or bind an argument from the input schema.

## Run a query

Open the query editor and click **Run**. Fill in the arguments from the input schema. Vettero saves that input on the query so the next run starts from the same values.

**Run** executes the saved query and shows the output. If you still have unsaved edits, Vettero saves them first. Insert, update, delete, and archive queries write real rows. Agents, functions, and widgets do not use this sample input — they pass their own arguments.

## Configure by type

### Select

Read rows from the table.

<table>
<thead>
  <tr>
    <th>
      Setting
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Mode
      </strong>
    </td>
    
    <td>
      <strong>
        By Row ID
      </strong>
      
       loads one row; <strong>
        By Query
      </strong>
      
       matches rows with filters
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Row ID
      </strong>
    </td>
    
    <td>
      Shown in By Row ID mode — the row to load (fixed or from an input)
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Filters
      </strong>
    </td>
    
    <td>
      Shown in By Query mode — conditions on table fields
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Pagination
      </strong>
    </td>
    
    <td>
      <strong>
        One
      </strong>
      
      , <strong>
        All
      </strong>
      
      , <strong>
        Limit/Skip
      </strong>
      
      , or <strong>
        Limit/Page
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Limit
      </strong>
      
       / <strong>
        Skip
      </strong>
      
       / <strong>
        Page
      </strong>
    </td>
    
    <td>
      Shown for Limit/Skip or Limit/Page
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Sorting
      </strong>
    </td>
    
    <td>
      Optional field and ascending/descending order
    </td>
  </tr>
</tbody>
</table>

**Returns** — By Row ID or pagination **One**:

```json
{ "row": { /* table fields */ }, "total": 1 }
```

Pagination **All** / **Limit/Skip** / **Limit/Page**:

```json
{ "rows": [ { /* table fields */ } ], "total": 0 }
```

### Insert

Add a new row.

<table>
<thead>
  <tr>
    <th>
      Setting
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Fields
      </strong>
      
       / <strong>
        Field values
      </strong>
    </td>
    
    <td>
      Map each table field to a fixed value or a query input
    </td>
  </tr>
</tbody>
</table>

**Returns:**

```json
{ "row": { /* inserted table fields */ } }
```

### Update

Change existing rows.

<table>
<thead>
  <tr>
    <th>
      Setting
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Mode
      </strong>
    </td>
    
    <td>
      <strong>
        By Row ID
      </strong>
      
       or <strong>
        By Query
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Row ID
      </strong>
      
       / <strong>
        Filters
      </strong>
    </td>
    
    <td>
      Which row(s) to update
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Update One
      </strong>
      
       / <strong>
        Update Many
      </strong>
    </td>
    
    <td>
      In By Query mode — update one match or many
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Fields
      </strong>
      
       / <strong>
        Field values
      </strong>
    </td>
    
    <td>
      Fields to change and their new values
    </td>
  </tr>
</tbody>
</table>

**Returns:**

```json
{ "success": true, "matchedCount": 0, "modifiedCount": 0 }
```

### Delete

Permanently remove rows.

<table>
<thead>
  <tr>
    <th>
      Setting
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Mode
      </strong>
    </td>
    
    <td>
      <strong>
        By Row ID
      </strong>
      
       or <strong>
        By Query
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Row ID
      </strong>
      
       / <strong>
        Filters
      </strong>
    </td>
    
    <td>
      Which row(s) to delete
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Delete One
      </strong>
      
       / <strong>
        Delete Many
      </strong>
    </td>
    
    <td>
      In By Query mode — delete one match or many
    </td>
  </tr>
</tbody>
</table>

**Returns:**

```json
{ "success": true, "matchedCount": 0, "modifiedCount": 0 }
```

### Archive

Soft-remove rows without deleting them. Configured like delete (**Archive One** / **Archive Many** in By Query mode).

**Returns:**

```json
{ "success": true, "matchedCount": 0, "modifiedCount": 0 }
```

### Aggregation

Group and summarize rows (count, sum, average, min, max).

<table>
<thead>
  <tr>
    <th>
      Setting
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Filters (WHERE)
      </strong>
    </td>
    
    <td>
      Optional conditions on rows before grouping
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Aggregations
      </strong>
    </td>
    
    <td>
      Operations to compute, with field and optional alias
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Group By
      </strong>
    </td>
    
    <td>
      Optional table field to group on
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Having
      </strong>
    </td>
    
    <td>
      Optional conditions on the grouped results
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Sort (ORDER BY)
      </strong>
    </td>
    
    <td>
      Optional sort on group results
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Pagination
      </strong>
    </td>
    
    <td>
      <strong>
        One
      </strong>
      
      , <strong>
        All
      </strong>
      
      , <strong>
        Limit/Skip
      </strong>
      
      , or <strong>
        Limit/Page
      </strong>
    </td>
  </tr>
</tbody>
</table>

**Returns** — each row has `_id` (group key) plus a property per operation alias:

```json
{
  "rows": [ { "_id": null, "count": 0 } ],
  "total": 0
}
```

## Attach to an agent

In the agent builder, open **Config → Tools → Manage Tools → Table**. Saved queries appear under **Global** (and agent-scoped queries from the builder panel). You can also attach table functions for one-off table ops.

You can also type `#` in the agent **Task** and select the query to mention and attach it.

## Use from functions and widgets

Bind the query as a **Query** resource, then call it:

- [Functions](https://docs.vettero.com/build/functions): `await vt.runQuery(key, input?)`
- [Widgets](https://docs.vettero.com/build/widgets): `vt.runQuery` or `vt.watchQuery` for live updates when related rows change

Callers pass a plain object whose keys match your input argument names.

## Limits

- Target table must exist in the workspace; filter fields must be real table fields.
- Type is chosen at create time and cannot change afterward.
- Result schema is regenerated on update — do not set it manually.
- Runs consume credits and are billed by affected or returned rows.
- Delete moves the query to [trash](https://docs.vettero.com/activity/trash) for the workspace retention period.

## Where queries are used

<table>
<thead>
  <tr>
    <th>
      Place
    </th>
    
    <th>
      How
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/agents">
          Agents
        </a>
      </strong>
    </td>
    
    <td>
      Tools → <strong>
        Table
      </strong>
      
       (saved queries), or <code>
        #
      </code>
      
       mention in the Task
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/functions">
          Functions
        </a>
      </strong>
    </td>
    
    <td>
      Bind as a <strong>
        Query
      </strong>
      
       resource and call <code>
        vt.runQuery
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/widgets">
          Widgets
        </a>
      </strong>
    </td>
    
    <td>
      Bind as a <strong>
        Query
      </strong>
      
       resource; call <code>
        runQuery
      </code>
      
       or <code>
        watchQuery
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/configuration/mcp-servers">
          MCP Servers
        </a>
      </strong>
    </td>
    
    <td>
      List queries on an MCP server; each becomes an MCP tool
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/workflows">
          Workflows
        </a>
      </strong>
    </td>
    
    <td>
      Invoke the saved query, or use inline <a href="https://docs.vettero.com/build/workflows/blocks/table">
        table blocks
      </a>
      
       for one-offs
    </td>
  </tr>
</tbody>
</table>


## Sitemap

See the full [sitemap](https://docs.vettero.com/sitemap.md) for all pages.
