Queries
Save SQL as a query, add parameters with {{ name }} and optional [[ … ]] parts, give it a chart, and clean up unused queries in one go.
A query is a saved question for one data source. It holds the SQL, an optional description, and at most one chart. Saved queries are what dashboards and notebooks are made of.
Create a query
There are a few ways to start one:
- Dashboards then All queries then New query, or press ⌘ K and choose New query. It opens an empty query on your workspace’s first data source. Change the data source from the picker next to the editor.
- Save as query in the SQL Editor.
- Query this table or New query on the Data page.
- Ask the agent to save a chart: it saves the query behind it.
The Queries page
Queries has no link of its own in the sidebar. Open it from Dashboards then All queries, from View queries in a dashboard’s ⋮ menu (which shows only that dashboard’s queries), or with ⌘ K.
- Columns: Name, Used on (the dashboards and notebooks that show it), Data source, Creator, Updated and Last run. Queries the agent saved show Agent as their creator.
- Search by name, description or dashboard, and switch between All, On a dashboard and Unused.
- Unused lists queries that aren’t on any dashboard or in any notebook, often leftovers from exploring. Tick the ones you don’t need and click Delete to clean up several at once.
The query page
- Name and description. Click the title to rename it. The description is optional: on dashboards, a chart shows an info icon that reveals it, so use it to say what the number means.
- Run (⌘ ↵) runs the query. Save (⌘ S) saves your changes. Changing the data source saves right away.
- Data Preview shows the result as a table; Chart turns it into a chart. See Charts.
- Ask AI (⌘ J) opens the agent beside the query, to explain it, read the result, or change the chart for you. See Ask about a page.
- Delete query is in the ⋮ menu. Dashboards that show its chart lose that widget.
Results
A query returns at most 5,000 rows and stops after 30 seconds. Orcabase keeps the latest result of every saved query, and dashboards show that saved result, so they open instantly without running anything. A result is replaced each time the query runs; older results aren’t kept.
Parameters
A parameter is a blank in your SQL that you fill in before running it. Write it as {{ name }}:
SELECT date_trunc('week', created_at) AS week, SUM(amount) AS revenue
FROM orders
WHERE country = {{ country }}
AND created_at >= {{ since }}
GROUP BY week
ORDER BY weekA box for each parameter appears above the editor. Type the values (VN, 2025-01-01) and run. Values are text: the database converts them where the SQL expects a number or a date.
How values are sent
| Data source | How a value reaches the database |
|---|---|
| PostgreSQL | As a real query parameter, separate from the SQL |
| BigQuery | As a named query parameter, separate from the SQL |
| DuckDB | Inserted into the SQL as quoted text. It’s escaped, so it can’t break out of the quotes, but it always arrives as text: use CAST({{ n }} AS INTEGER) where you need a number. |
Optional parts
Wrap a condition in [[ … ]] to make it optional. The part is kept, without the brackets, when every parameter inside it has a value, and dropped when one is empty:
SELECT count(*) AS orders
FROM orders
WHERE true
[[AND country = {{ country }}]]
[[AND created_at >= {{ since }}]]Leave country empty and the query counts every country. Brackets with no parameter inside, like arr[[1]], are left alone.
Note
Parameter values aren’t saved with the query. A dashboard shows the chart from the query’s last run, and Run all queries and scheduled refreshes run each query with its parameters empty. Make each parameter optional with [[ … ]] so the query still works then. Dashboard filters can fill parameters for you: a date range fills {{ name_start }} and {{ name_end }}, a number range {{ name_min }} and {{ name_max }}.
Metric queries
A query can also ask for metrics by name instead of holding SQL: revenue and orders, by month, last 90 days. Orcabase writes the SQL each time it runs, from the current metric definitions, so the chart stays right when a definition changes. See Explore and query metrics.
Something unclear or missing? Troubleshooting covers the common errors, or message us.