# queries

## Description

Exposes metadata about all streaming queries in the organization — both active and terminated. Useful for auditing query history, checking current state, or finding errored queries. Rows are filtered to queries the current role has USAGE privileges on.

{% hint style="info" %}
**Note** `current_state` is the live observed state; `intended_state` is what was requested.
{% endhint %}

## Syntax

```sql
SELECT ... FROM deltastream.sys."queries";
```

## Columns

| Column                    | Type           | Nullable | Description                                                                                    |
| ------------------------- | -------------- | -------- | ---------------------------------------------------------------------------------------------- |
| `id`                      | VARCHAR        | No       | Unique identifier of the query.                                                                |
| `organization_id`         | VARCHAR        | No       | The unique identifier of the organization this resource belongs to.                            |
| `owner`                   | VARCHAR        | No       | The role that owns this resource.                                                              |
| `name`                    | VARCHAR        | Yes      | Name of the query, if it was created as a named query.                                         |
| `version`                 | INTEGER        | Yes      | Version number of the named query.                                                             |
| `sql`                     | VARCHAR        | No       | The SQL statement of the query.                                                                |
| `is_interactive`          | BOOLEAN        | No       | Whether the query is an interactive (sandbox) query.                                           |
| `intended_state`          | VARCHAR        | No       | The state the query has been instructed to be in. One of `running`, `stopped`.                 |
| `current_state`           | VARCHAR        | No       | The observed current state of the query. One of `running`, `stopped`, `errored`, `terminated`. |
| `created_by_role`         | VARCHAR        | No       | The role that created this resource.                                                           |
| `created_by_role_deleted` | BOOLEAN        | No       | Whether the creating role has since been deleted.                                              |
| `created_by`              | VARCHAR        | No       | The user that created this resource.                                                           |
| `created_at`              | TIMESTAMP\_LTZ | No       | Timestamp when this resource was created.                                                      |
| `updated_by_role`         | VARCHAR        | No       | The role that last updated this resource.                                                      |
| `updated_by_role_deleted` | BOOLEAN        | No       | Whether the last updating role has since been deleted.                                         |
| `updated_by`              | VARCHAR        | No       | The user that last updated this resource.                                                      |
| `updated_at`              | TIMESTAMP\_LTZ | No       | Timestamp when this resource was last updated.                                                 |
| `deleted_at`              | TIMESTAMP\_LTZ | Yes      | Timestamp when the query was terminated and deleted, if applicable.                            |

## Examples

List all queries:

```sql
SELECT * FROM deltastream.sys."queries";
```

```sh
[{"id":"7e36ae90-da7c-4829-ae6e-ce251c583332","organization_id":"00000000-0000-0000-0000-000000000001","owner":"sysadmin","name":null,"version":null,"sql":"select * from rgc_pageviews_pb;","is_interactive":true,"intended_state":"running","current_state":"terminated","created_by_role":"sysadmin","created_by_role_deleted":false,"created_by":"user@example.com","created_at":"2025-09-23T16:36:50.152Z","updated_by_role":"sysadmin","updated_by_role_deleted":false,"updated_by":"user@example.com","updated_at":"2025-09-23T16:36:50.152Z","deleted_at":"2025-09-23T17:06:52.8Z"}]
```

Filter to only running queries:

```sql
SELECT id, name, sql, current_state FROM deltastream.sys."queries" WHERE current_state = 'running';
```

Find errored queries:

```sql
SELECT id, name, sql, current_state FROM deltastream.sys."queries" WHERE current_state = 'errored';
```

## See Also

* [System Tables Overview](https://docs.deltastream.io/reference/sql-syntax/systables)
* [LIST QUERIES](https://docs.deltastream.io/reference/sql-syntax/command/list-queries)
