Skip to content

AI SQL generation

How Data Explorer uses schema context, AI routing, SQL Steps, and typed recommendations to turn natural-language requests into usable SQL.

Current SQL generation flow

Data Explorer is no longer a simple Gemini-only flow. The app now builds SQL generation in three layers: capture schema context from the active connection, route the request through the AI router, then normalize the output so the editor and action cards can use it safely.
01
Schema-aware context

The backend extracts metadata from the active connection: tables, columns, relationships, and part of the query context. The AI is not forced to guess table and column names from thin air.

02
Provider-aware routing

The routing mode in the UI is only one layer. The real chain also depends on any explicit model/provider selection, whether the prompt includes an image, and whether the request is detected as current-information work. For normal SQL tasks, the app prefers a safe text-capable chain.

03
Structured output + action cards

If the task is SQL/query work, the backend tries to return clear SQL, an explanation, and recommendation cards such as `query_fix`, `index_suggestion`, `schema_suggestion`, or `chart_suggestion` so the UI can take the next step.

The AI SQL model role

Inside `Configure AI`, the `AI SQL` role lets you separate the model used for SQL generation from the general assistant model. This is worth using when you want chat to stay conversational while SQL stays optimized for reasoning, syntax, or speed.

Inherit the Assistant model

Best when you want to keep configuration simple and manage one main model for most workflows.

Use a dedicated SQL model

Best when you want AI SQL to use a different model for optimization, query fixing, index suggestions, or long join-heavy statements.

Routing modes

Auto

The default mode for most SQL work. The app starts from the appropriate auto chain and filters it by capability instead of always jumping straight to the most expensive lane.

Fast / Cheap

Leans toward quicker iteration and cheaper lanes, but does not override an explicit provider if you already locked the model.

Best Quality

Useful when a task needs heavier reasoning, explanation, rewriting, or deeper optimization. It leans toward stronger lanes earlier.

Gemini Only

Always uses the Gemini model you selected. Useful when you want the most stable output or want to avoid other providers entirely.

One easy misunderstanding: if a prompt is detected as a current-information request such as “latest”, “today”, or “current price”, the router leans toward live-search chat behavior instead of treating it as a pure SQL generation request.

SQL Steps / sequential multi-statement execution

When a script contains multiple SQL statements, you can open `SQL Steps` to split the script into blocks, drag and reorder them, edit each step, add new ones, then apply the sequence back to the editor or run it sequentially in the arranged order.

When should you use it?

Great for setup scripts, data seeding, create-table plus insert plus index plus analyze flows, or any workflow where execution order matters.

What can you do in the dialog?

Drag up and down, use move up/down buttons, delete steps, add a new step, edit each step directly, then choose `Apply to editor` or `Run sequence`.

Example multi-step workflow

# A common SQL Steps script

CREATE TABLE perf_orders_200k_v1 (...);

INSERT INTO perf_orders_200k_v1 (...) SELECT ...;

CREATE INDEX idx_perf_orders_tenant_id ON perf_orders_200k_v1(tenant_id);

ANALYZE perf_orders_200k_v1;

Effective prompt patterns

The best prompts are intention-clear prompts: ask for a new SQL statement, an optimization pass, an index suggestion, or a chart suggestion from a result set.
Generate a new query

# Sample prompt

List top 10 customers by spending in Q4 2025 with order count and total revenue.

Optimize + suggest indexes

# Sample prompt

Optimize this query and give me actionable index recommendations with runnable SQL if possible.

Chart suggestion

# Sample prompt

Suggest the best chart for this result set and explain which fields should be used.

Action cards & execution safety

Recommendation cards under the AI bubble can insert SQL into the editor or run it directly when the card carries the right payload. However, `Insert into editor` only inserts the SQL, while `Run suggestion` is the execution path and the app asks for confirmation before running AI-generated SQL.

AI can still suggest the wrong column, the wrong table, or a statement that does not match your real data. Review SQL before running it, especially for DDL, index creation, or operations that can affect performance.

Ready to explore data?

Join the community on GitHub to contribute to the smartest local database IDE.

Launch App