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

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