> ## 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
```

Saved queries print as a table with a one-line preview of each query's SQL. Pass `-o json` or `-o csv` for scripting:

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

## 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`](https://docs.oleander.dev/cli/lake#write-results-with-query-submit) writes the **result set** to a table. They are different things.

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

```bash theme={null}
oleander queries get top_customers -o 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"
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.