> ## Documentation Index
> Fetch the complete documentation index at: https://developers.zite.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run SQL

> Execute a read-only SQL query against a database and its linked tables.

<Note>
  Queries are **read-only**. Only `SELECT` statements are permitted — any write, DDL, or transaction-control statement is rejected.
</Note>

## Writing queries

* **Human-readable names.** Reference tables and fields by the same names you see in the Zite SDK, not physical column names (e.g. `SELECT "Name" FROM "Orders"`).
* **Bind parameters.** Pass user-supplied values in `params` and reference them with positional placeholders (`$1`, `$2`, ...). This is safer than interpolating values into the SQL string.
* **Linked records.** Join through link tables to traverse relationships. Use [Get SQL Schema](/api/sql/get-schema) to discover table names, field names, and link tables.

```json theme={null}
{
  "sql": "SELECT \"Name\", \"Amount\" FROM \"Orders\" WHERE \"Status\" = $1 ORDER BY \"Amount\" DESC",
  "params": ["Paid"]
}
```

## Limits

| Behavior   | Detail                                                                                                               |
| ---------- | -------------------------------------------------------------------------------------------------------------------- |
| Row cap    | Results are capped at \~2000 rows. When more match, `truncated` is `true` and only the first 2000 rows are returned. |
| Timeout    | `statementTimeoutMs` defaults to `10000` (10 seconds) and is capped at `60000` (60 seconds).                         |
| Query size | The `sql` string may be up to 50,000 characters; `params` accepts up to 1000 values.                                 |

<Tip>
  If `truncated` is `true`, narrow the query with a `WHERE` clause or add your own `LIMIT`/`OFFSET` to page through results.
</Tip>


## OpenAPI

````yaml api/openapi.json POST /bases/{databaseId}/sql
openapi: 3.0.1
info:
  title: Zite DB REST API
  description: >-
    A REST API for managing Zite databases, tables, fields, and records. Build
    database-driven applications with programmatic access to your structured
    data.


    ## What is REST API


    A **REST API** simplifies interaction with your database data, allowing for
    automation and integration. Following RESTful principles, it seamlessly
    connects to retrieve, create, and modify database data.


    ## Getting Started


    ### Authentication

    All API requests require authentication using your Zite API key in the
    Authorization header:

    ```

    Authorization: Bearer YOUR_API_KEY

    ```


    ### Base URL

    https://tables.zite.com/api/v1


    ### Rate Limits

    API requests are rate limited to ensure service quality. Standard rate
    limits apply to all endpoints.


    ### Error Handling

    The API returns standard HTTP status codes and JSON error responses with
    detailed error messages.
  version: 1.0.0
servers:
  - url: https://tables.zite.com/api/v1
    description: Zite DB API
security:
  - bearerAuth: []
tags:
  - name: Databases
    description: Operations for managing databases
  - name: Tables
    description: Operations for managing tables within databases
  - name: Fields
    description: Operations for managing fields within tables
  - name: Records
    description: Operations for managing records within tables
  - name: Webhooks
    description: >-
      Operations for managing webhook subscriptions to receive real-time
      notifications when database events occur
  - name: SQL
    description: Run read-only SQL queries against a database and inspect its SQL schema
paths:
  /bases/{databaseId}/sql:
    post:
      tags:
        - SQL
      summary: Run SQL query
      description: >-
        Executes a read-only (SELECT) SQL query against the database. Tables and
        fields are referenced by their human-readable names. Results are capped
        at 2000 rows.
      operationId: runSql
      parameters:
        - name: databaseId
          in: path
          required: true
          schema:
            type: string
          description: The unique identifier of the database
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SqlRequest'
      responses:
        '200':
          description: Query results
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SqlResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  schemas:
    SqlRequest:
      type: object
      properties:
        sql:
          type: string
          maxLength: 50000
          description: >-
            The read-only SELECT statement to execute. Reference tables and
            fields by their human-readable names (the same names used by the
            Zite SDK), not physical column names. Bind user-supplied values with
            positional placeholders (`$1`, `$2`, ...) rather than string
            interpolation.
        params:
          type: array
          maxItems: 1000
          items:
            description: >-
              A JSON value (string, number, boolean, or null) bound to the
              matching `$1`, `$2`, ... placeholder.
          description: Ordered bind values for the `$1`, `$2`, ... placeholders in `sql`.
        statementTimeoutMs:
          type: integer
          minimum: 1
          maximum: 60000
          default: 10000
          description: >-
            Per-statement timeout in milliseconds. Defaults to 10000 (10
            seconds). Capped at 60000 (60 seconds).
      required:
        - sql
      example:
        sql: >-
          SELECT "Name", "Amount" FROM "Orders" WHERE "Status" = $1 ORDER BY
          "Amount" DESC
        params:
          - Paid
    SqlResponse:
      type: object
      properties:
        rows:
          type: array
          items:
            type: object
            additionalProperties: true
          description: The result rows. Each row is an object keyed by result column name.
        columns:
          type: array
          items:
            $ref: '#/components/schemas/SqlColumn'
          description: Ordered metadata describing each result column.
        rowCount:
          type: integer
          description: The number of rows returned (after any truncation).
        truncated:
          type: boolean
          description: >-
            True when the result exceeded the 2000-row cap and was truncated.
            Narrow the query or add a LIMIT to avoid truncation.
      required:
        - rows
        - columns
        - rowCount
        - truncated
    SqlColumn:
      type: object
      properties:
        name:
          type: string
          description: >-
            Column name as it appears in the result rows. Derived from the
            SELECT clause (alias or field name).
        originalName:
          type: string
          description: The original underlying column name before any aliasing was applied.
      required:
        - name
        - originalName
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              description: Error code
            message:
              type: string
              description: Human-readable error message
          required:
            - code
            - message
      required:
        - error
  responses:
    BadRequest:
      description: Invalid request data or validation failure
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            invalidRecordId:
              value:
                error:
                  code: INVALID_RECORD_ID
                  message: Record ID is not in valid UUID format
            validationError:
              value:
                error:
                  code: BAD_REQUEST
                  message: Invalid request data or validation failure
    Unauthorized:
      description: Invalid or missing API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: UNAUTHORIZED
              message: Invalid or missing API key
    NotFound:
      description: Requested resource does not exist
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          example:
            error:
              code: NOT_FOUND
              message: Requested resource does not exist
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: 'Enter your Zite API key. Format: Bearer <api_key>'

````