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

# SQL editor

> Write and execute SQL queries in Basedash

While Basedash excels at creating charts through natural language, you can also write custom SQL queries when you need more control or complex analysis.

## The SQL editor

![The SQL editor with a revenue by channel query using date and channel variables, the schema browser listing the warehouse tables, and the resulting stacked bar chart below](https://mintcdn.com/basedash/aH3kb-Bl5OLwdg2n/images/features/sql-editor/hero.webp?fit=max\&auto=format\&n=aH3kb-Bl5OLwdg2n\&q=85\&s=9b945c83d33aded735b5d68f5eaf62a7)

### Features

* Syntax highlighting
* Auto-completion
* Schema browser
* Query history
* Result previews
* Performance metrics

## Writing queries

### Basic query structure

```sql theme={"dark"}
SELECT column1, column2
FROM your_table
WHERE condition
GROUP BY column1
ORDER BY column2 DESC
LIMIT 100
```

### AI query assistant

Need help writing SQL? Our AI can:

* Generate queries from natural language
* Explain complex queries
* Suggest optimizations
* Fix syntax errors

Just describe what you want to query, and the AI will help write the SQL.

## Query best practices

### Performance

* Use appropriate LIMIT clauses
* Avoid SELECT \*
* Index important columns
* Use efficient joins

### Organization

* Format queries for readability
* Add comments for complex logic
* Use CTEs for better structure

## Working with results

### Visualization

* Convert results to charts
* Export to CSV/Excel
* Share query results
* Save as dashboard components

### Result freshness

Results refresh when you rerun a query or change inputs. Basedash may temporarily cache some results in your browser to improve performance.

## Advanced features

### Variables

Use variables in your queries:

```sql theme={"dark"}
SELECT *
FROM orders
WHERE date >= {{start_date}}
  AND date <= {{end_date}}
```

## Security

* Read-only access by default
* Query history logging
* Resource usage limits
* Data access controls

## FAQ

> [accordion] Can I run INSERT, UPDATE, or DELETE statements?
>
> No. The SQL editor runs a single `SELECT` statement at a time. On Postgres,
> Redshift, and Supabase, people with **Can edit** access to a data source can
> ask chat to make a change, and it only runs after they approve it. See
> [Accuracy and trust](https://basedash.com/docs/features/accuracy-and-trust.md#read-only-by-default).

> [accordion] Which SQL dialect do I write?
>
> The data source's own dialect, such as PostgreSQL, BigQuery, or Snowflake
> SQL. Basedash runs your query on the data source as written.

> [accordion] Can I join tables from two different data sources?
>
> Not in one query. A [multi-source chart](https://basedash.com/docs/features/multi-source-charts.md) runs
> a separate query on each data source and then joins the results.

> [accordion] How many rows can a query return, and when does it time out?
>
> Queries return up to 10,000 rows, so aggregate in SQL instead of pulling raw
> rows into a chart. On Postgres, queries time out after about two minutes.
> Admins can change the timeout for each data source in [connection
> settings](https://basedash.com/docs/data-sources/connection-settings.md#query-timeouts).

> [accordion] Can I go back to an earlier version of a query?
>
> Yes. Every chart keeps a query history. Open the chart's menu, choose
> **Query history**, pick a version, and click **Revert to query**. Each
> version shows who saved it and whether the AI wrote it.


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