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

# SQL generator

> Convert natural language to SQL queries and explore your data with AI.

Sourcetable's AI writes SQL queries from plain English. This works for data in your spreadsheet and for connected databases.

## Using the SQL generator

Open the AI chat and select **Query** mode, or ask directly in the default mode. The AI generates the SQL, runs it, and returns results to your spreadsheet.

### Example prompts by complexity

| Complexity           | Example prompt                                                            |
| -------------------- | ------------------------------------------------------------------------- |
| **Basic**            | "Show me all orders from January"                                         |
| **Filtering**        | "Find customers with more than 5 orders and total spend over \$10,000"    |
| **Aggregation**      | "What's the average revenue by product category, broken down by quarter?" |
| **Window functions** | "Rank each salesperson by their monthly revenue within their region"      |
| **Cohort analysis**  | "Show monthly retention rates for each customer cohort by signup month"   |

## How it works

<Steps>
  <Step title="You describe what you want">
    Type a natural language description of the data you need: "Show me the top 10 customers by lifetime value who signed up this year."
  </Step>

  <Step title="AI generates SQL">
    The AI writes a SQL query targeting your spreadsheet data or connected database.
  </Step>

  <Step title="Query executes">
    The query runs against your data via DuckDB (for spreadsheet data) or directly on connected databases.
  </Step>

  <Step title="Results returned">
    Results appear in your spreadsheet. You can ask follow-up questions to refine the output.
  </Step>
</Steps>

## Iterative refinement

You can refine queries conversationally:

1. "Show me monthly revenue by region" — AI generates the initial query
2. "Add a year-over-year growth column" — AI modifies the query
3. "Filter to only regions with growth over 10%" — AI adds the condition
4. "Make it a chart" — AI creates a visualization from the results

The AI remembers the context of your previous queries in the conversation, so each follow-up builds on the last.

## Error handling

If a query fails (syntax error, missing column, type mismatch), the AI:

1. Reads the error message
2. Diagnoses the problem
3. Rewrites and re-executes the corrected query
4. Returns the results

You don't need to debug SQL errors — the AI self-corrects.

## Cross-source queries

When you have data in your spreadsheet and a connected database, you can query across both:

* "Compare the revenue in my spreadsheet with the production database totals"
* "Join my local customer list with the orders table in PostgreSQL"

The AI determines the best execution strategy and handles the cross-source join.

## When to use AI SQL vs. the SQL editor

| Use AI SQL generator when...                            | Use the [SQL editor](/data/sql-editor) when... |
| ------------------------------------------------------- | ---------------------------------------------- |
| You want quick answers without writing SQL              | You know the exact query you need              |
| You're exploring data and aren't sure what to look for  | You want to save queries to the data library   |
| You need to iterate and refine results conversationally | You prefer the visual query builder            |
| You're working across multiple data sources             | You need precise control over query syntax     |

## Data library

Save AI-generated queries for reuse. After the AI runs a query:

1. Click **Save to Library** on the result
2. Name and categorize the query
3. Access it later from the data library sidebar
4. Schedule queries to refresh automatically with [automated reporting](/ai/automated-reporting)
