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

# Queries

> Save, retrieve, and manage named SQL queries from the CLI.

Saved queries let you store and share frequently used SQL expressions. They are scoped to your organization.

## List saved queries

```bash theme={null}
oleander queries list
```

Pass `--json` for machine-readable output:

```bash theme={null}
oleander queries list --json
```

Example output:

```
Saved queries
- name: top_customers
  id: sq_abc123
  updated: 2026-05-01T12:00:00Z
  by: alice
  query: SELECT customer_id, sum(revenue) AS total FROM orders ...
```

## Get a saved query

Print the full details of a saved query by name:

```bash theme={null}
oleander queries get <name>
```

To print only the SQL (useful for piping into `oleander query`):

```bash theme={null}
oleander queries get <name> --query-only
```

**Example - run a saved query directly:**

```bash theme={null}
oleander query "$(oleander queries get top_customers --query-only)"
```

To write its results to a table instead of printing them, send the same SQL to `oleander query submit`:

```bash theme={null}
oleander query submit "$(oleander queries get top_customers --query-only)" \
  --destination default.top_customers_snapshot
```

<Note>
  `oleander queries save` stores the **SQL text** under a name. [`oleander query submit --destination`](/cli/lake#write-results-with-query-submit) writes the **result set** to a table. They are different things.
</Note>

Pass `--json` to get the raw JSON response:

```bash theme={null}
oleander queries get top_customers --json
```

## Save a query

```bash theme={null}
oleander queries save <name> "<sql>"
```

If a query with the same name exists it is replaced.

**Example:**

```bash theme={null}
oleander queries save top_customers \
  "SELECT customer_id, sum(revenue) AS total FROM oleander.default.orders GROUP BY 1 ORDER BY 2 DESC LIMIT 20"
```
