Generate SQL with Ask AI

Dory SQL generation is built into SQL Console, not a detached prompt box. Depending on where you ask, Dory can use the active connection, database, SQL dialect, editor text, selection, recent execution, result shape, and schema context. Generated SQL is returned to the editor or Copilot panel first, so you can review it before running it.

Choose an Entry Point

Entry pointBest forHow it enters your workflow
/ in the editorAdd a query, filter, aggregation, or CTE while writing SQL.Dory inserts a comment with your request and the generated SQL at the cursor.
Toolbar Ask AIStart from an empty tab or draft the first version of a query.Generated SQL lands in the active SQL tab for review, formatting, and execution.
Right-side Ask AI panelExplain current SQL, continue from results, fix errors, or ask follow-up questions.The panel carries editor and result context; SQL changes can be applied back to the editor.
Copilot ActionsFix, optimize, rewrite, or aggregate existing SQL.Preview the generated SQL, then replace the current SQL or open it in a new tab.

If you do not know the right table yet, start with Explore Unknown Tables or the table structure view in Explorer.

Inline Ask AI

Type / on an empty line in the SQL editor, or click Ask AI in the toolbar. If the current line already has SQL text, / is inserted as a normal character so it does not interrupt the statement you are writing.

Inline Ask uses:

  • The full editor text in the current SQL tab.
  • The current database and SQL dialect.
  • The table names you mention in the prompt or already have in the editor.
  • Your natural-language request as the instruction for the generated SQL.

After submission, Dory generates SQL only. It does not execute the query. The inserted block includes a comment so you can see which request created it:

Count new users per day for the last 30 days, ascending by date, return only date and users.

Generated output looks like:

-- Count new users per day for the last 30 days, ascending by date, return only date and users.
select
  date(created_at) as date,
  count(*) as users
from users
where created_at >= current_date - interval '30 days'
group by 1
order by 1 asc;

The actual syntax is adapted to the active connection type, such as PostgreSQL, MySQL, SQL Server, Oracle, SQLite, ClickHouse, Doris, DuckDB, or MariaDB.

Ask AI Panel

Press Cmd/Ctrl + I to open the SQL Console Ask AI panel. Use this when you want a conversation instead of a one-shot insert. The panel receives Copilot Context:

  • Current editor text and selection.
  • Inferred database, schema, and tables.
  • Recent result columns, types, row count, limits, and result profile.
  • Current SQL dialect and available schema context.

You can ask:

Explain each step in the current SQL and point out where duplicate counting may happen.

Or continue from the current result:

Rewrite this as a weekly aggregation and keep the same filters.

When the question needs data, the Ask AI panel first generates read-only SQL and can run it through Dory's controlled sqlRunner. It does not fabricate results. If SQL is rejected, fails, or only returns a sample, the panel uses the error and result metadata to repair the query or explain the limitation.

Copilot Actions

When you already have SQL or a failed execution, use Actions in the right panel:

ActionUse when
Fix SQL errorsExecution failed and AI should use the error, dialect, and original SQL to repair it.
Optimize performanceThe query works but scans too much, needs stronger filters, or should preserve the same output with less cost.
Rewrite SQLYou want clearer SQL, a different style, or a dialect adjustment.
Convert to aggregationDetailed rows need to become analysis-ready or chart-ready aggregate output.

Actions generate a reviewable SQL preview first. You choose whether to replace the current editor content or open the result in a new tab.

Write Better Requests

Good requests are short but specific about result shape and boundaries:

Use the events table to count DAU for the last 14 days.
Fields: user_id, event_time, event_name.
Only count event_name = 'app_open'.
Return date and dau, ordered by date ascending.
Do not SELECT *; only scan the required time range.

If you are editing existing SQL, you can be shorter:

Based on the current SQL, add grouping by country and keep only the top 20.

For errors:

This SQL fails in PostgreSQL with: column "created_date" does not exist.
Fix the field name using the current schema and preserve the same metric definition.

Review Before Running

Review the generated SQL before treating it as correct:

  1. Are table and column names real?
  2. Is the time range correct?
  3. Does the aggregation match the business definition?
  4. Does the query need deduplication?
  5. Are filters or limits present?
  6. Does the syntax match the current database?
  7. Does it project only the needed columns instead of using SELECT *?

Generate Chart-Ready SQL

Rewrite this query for a bar chart. Return only category and value, order by value descending, and limit to the top 10.

FAQ

Can AI-generated SQL run directly in production?

Do not run it blindly. Inline Ask and Actions generate SQL without automatically executing it. Review carefully, especially for production databases, large tables, missing time filters, and any non-readonly operation.

Why does AI reference missing columns?

Confirm the table in Explorer, then name the table and columns in your request or ask Dory to regenerate from the current table structure. The right-side Ask AI panel has the richest schema and result context; Inline Ask is best when you already know the table names you want to use.

How can I make AI generate faster SQL?

Describe partition fields, time range, required columns, and performance goals. For existing SQL, use Optimize performance in Copilot Actions.

Next Steps

How is this guide?