> ## Documentation Index
> Fetch the complete documentation index at: https://lightdash-docs-wall-clock-timezone.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL runner

> Build ad-hoc queries and charts with totally custom SQL

A query built in the SQL runner can be:

* used to power a single saved chart
* turned into a virtual view so it becomes a reusable table
* written back to dbt as a model

Only users with [`developer or admin access`](/workspace-admin/roles) can use the SQL runner.

## Getting started with the SQL Runner

The SQL Runner can be accessed from the `New` --> `Query using SQL Runner` option in your navigation bar.

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/access-sql-runner-e299a63e83e2ecb4b3b09c64f94211be.jpg?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=b870922fecbb9587bc9b14d1e26d02c0" alt="The New menu open in the navigation bar, with Query using SQL Runner in the list" width="2734" height="1462" data-path="images/explore/sql-runner/access-sql-runner-e299a63e83e2ecb4b3b09c64f94211be.jpg" />
</Frame>

Once you're in the SQL Runner, you'll see four key components:

1. A list of tables in your data warehouse that Lightdash has access to (based on your data warehouse connection in Lightdash).
2. The schema of a selected table.
3. The SQL query builder.
4. The results from your latest SQL query.

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/sql-runner-components-60eb488682b46fb2ecedad92a2656adb.jpg?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=5ccec370b25a8e758732bdbf306861c8" alt="The SQL Runner laid out in four parts: the warehouse table list, the selected table's schema, the SQL query builder, and the results below" width="2705" height="1478" data-path="images/explore/sql-runner/sql-runner-components-60eb488682b46fb2ecedad92a2656adb.jpg" />
</Frame>

To build a query in the SQL Runner, just write your query in the SQL query builder, then hit `Run query` to see the results.

You can access your **Query history** from your session by clicking on the `SQL Query history` button beside `Run query`.

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/query-history-3c6295f2352f2d118474de3a51d83fd7.jpg?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=2d737e77c675bef603b18b7f6965804f" alt="The SQL Query history panel listing the queries run in this session, opened from the button beside Run query" width="3288" height="1216" data-path="images/explore/sql-runner/query-history-3c6295f2352f2d118474de3a51d83fd7.jpg" />
</Frame>

Once you've run your query, you can either:

1. [**Build and save a Chart**](#saved-charts-in-the-sql-runner)
2. [**Create a Virtual View so your query becomes a reusable table in Lightdash**](/semantic-layer/virtual-views)
3. [**Write-back to dbt so your query is saved as a governed model.**](/integrations/dbt/write-back)
4. Click the link icon in the top-right to share your query draft with a coworker (or save it yourself to return to later).

## Saved charts in the SQL Runner

Once you've run your query, you can build a chart by clicking on the `chart` tab in the SQL Runner.

The charts in the SQL Runner are built from the data that you generated in your query. The chart builder automatically aggregates the data from your query results using the aggregation type that you choose. Depending on the column type, the aggregation options are:

* count (which is a distinct count and will ignore duplicates)
* any (which will count and include duplicates)
* sum
* average
* max
* min

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/build-chart-sql-runner-72c3e95c8e37b83f48189599e25ddba1.jpg?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=2adb19ef4306a67c80a0e6c594d09fd2" alt="The chart builder in the SQL Runner, with field pickers and an aggregation drop-down beside a chart of the query results" width="2699" height="1482" data-path="images/explore/sql-runner/build-chart-sql-runner-72c3e95c8e37b83f48189599e25ddba1.jpg" />
</Frame>

Once you've configured your chart, you can save it, add it to a space, and add it to a dashboard by hitting `save chart`.

SQL runner charts on a dashboard can be filtered in the UI. See the [dashboard filter documentation](/explore/dashboards/filter#filtering-charts-created-in-the-sql-runner) for more information.

### Chart types

The SQL runner supports the following chart types:

* Table
* Bar chart
* Line chart
* Pie chart
* Big number

### Series draw order

For bar, line, and mixed charts, open **Chart → Series** to choose which series appear in front of others. The list runs from **Back → Front**: a series lower in the list is drawn on top of a series above it. The last series has a **Front** badge.

* **Drag** a series by its handle to change its position.
* **Bring to front** moves a series to the end of the list. Hover over its heading to reveal the button.
* **Reverse** flips the order in that list.

For a mixed chart with a line hidden behind bars, bring the line series to the front. Its color, label, chart type, and axis assignment stay with it.

<Frame caption="The Target line is at the front of the series list, so it remains visible over the Sales bars.">
  <img alt="SQL Runner Series settings with Sales bars followed by a Target line marked Front, and the line visible across the bars" className="block dark:hidden" src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/series-draw-order-light.png?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=f174336fd595ee99c805153fd597ba3a" width="1600" height="1100" data-path="images/explore/sql-runner/series-draw-order-light.png" />

  <img alt="SQL Runner in dark mode, with the Target line marked Front and visible across the Sales bars" className="hidden dark:block" src="https://mintcdn.com/lightdash-docs-wall-clock-timezone/nFkXhJpubVE-F3wW/images/explore/sql-runner/series-draw-order-dark.png?fit=max&auto=format&n=nFkXhJpubVE-F3wW&q=85&s=957c0eda12742345b071f14bbde3c475" width="1600" height="1100" data-path="images/explore/sql-runner/series-draw-order-dark.png" />
</Frame>

When using **Group by**, reorder whole metric groups or expand a group to reorder its individual series.

Charts keep their current order until you change it. **Save** the chart to retain your chosen order when reopening it or viewing it on a dashboard.

## Limitations

### ORDER BY not guaranteed

Results from the SQL runner are not guaranteed to respect your `ORDER BY` clause. This is because Lightdash wraps your query in a subquery to support dashboard filtering, and SQL databases don't preserve ordering from subqueries.

<Note>
  **Common symptoms:** Results appear in wrong order, sorting doesn't work, ORDER BY ignored, sort order not preserved.
</Note>
