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

# SQL

> Query your sheet with real SQL, entirely in-browser, and connect external databases.

Tablix includes a built-in SQL editor that lets you query your spreadsheet data with real SQL — no external database required.

## Opening the SQL Editor

Open the code editor sidebar and choose **SQL** as the language. Like the Python editor, it's Monaco-based with syntax highlighting, a resizable console, dark mode, and an in-editor AI prompt box for generating queries from plain English.

## How It Works

Your active sheet's data is loaded into an in-memory **SQLite database** (via `sql.js`, SQLite compiled to WebAssembly) running in a background Web Worker — entirely in your browser.

<Info>
  * The table is always named **`sheet`**
  * Column headers are auto-**sanitized** into valid sQL identifiers (spaces/special characters replaced, duplicate names de-duplicated, blank headers auto-named by column letter)
  * All columns are loaded as text; use SQLite's date/string functions as needed for filtering and comparisons
</Info>

## Example

```sql theme={null}
SELECT Country, SUM(Profit) as Total_Profit
FROM sheet
GROUP BY Country
ORDER BY Total_Profit DESC
LIMIT 5
```

Running a query writes the result set back into the grid and also shows a preview table in the console.

## AI-Generated SQL

Ask the AI Assistant (or the in-editor AI prompt) for what you want in plain English, and it generates a SQL query against the `sheet` table using your actual column names, then runs it.

<Tip>
  The AI is instructed to always use `GROUP BY`/`SUM` (rather than picking a single row) when you ask for "highest"/"top" values by category.
</Tip>

## External Databases

Beyond querying your local sheet, Tablix can connect to and pull tables in from external sources via the **Database** sidebar:

<CardGroup cols={3} />

Once connected, saved connections appear under "Connected Sources" in the Database sidebar. Browse available tables/schemas and load a table into your sheet — after which you can query, chart, or run Python/AI analysis on it just like any other sheet data.

<Tip>
  See [Error Handling](/error-handling#sql-errors) for SQL error messages and what causes them.
</Tip>
