By the end of this guide you will have taken a question about your data, such as "which customers ordered twice last month", and turned it into a SQL query that you have read, tested on safe data and run. The job is done by a text-to-SQL tool: you describe the question in plain language, the tool uses your schema to write the query, and you check it before trusting the numbers.
If you want to write SQL queries with AI, the main choices are whether the tool only drafts SQL for you to run yourself, or connects to your database and runs it for you. This guide covers both and compares seven tools from our Coding category.
Key takeaways
- Give the tool your schema (tables, columns and relationships), ask one clear question, read the generated SQL, and test it on a small or read-only copy before relying on it.
- The main decision is how much access the tool gets: a query writer that works from a schema you provide, a tool with read-only database access, or a self-run server that connects your AI assistant to the database.
- Free options are small: for example AI2SQL's Free plan covers 3 SQL queries a day on a demo database, and dbForge AI Assistant has a free Express license with 200,000 tokens a month.
- Generated SQL can be wrong in ways that look right. A person who understands the data must verify results used for decisions, finance or reporting.
What you need before you start
- A question you can state clearly. Include the time period, the filter and the result you expect, such as a count, a list or a monthly total.
- Your schema. Table names, column names, data types and how tables join. A diagram or a dump of the structure (without the data) is enough.
- A safe place to run queries. A read-only database user, a replica or a test copy, not your live production system.
- Your database engine. MySQL, PostgreSQL, SQL Server, Oracle and others differ in syntax, so tell the tool which one you use.
- Time. Setup and a first query take under an hour.
- Difficulty. Easy for simple questions, moderate when joins and aggregates get involved.
- Data care. Do not paste personal or confidential records into a hosted tool without checking its data terms.
Which kind of AI tool fits this job
Pick by how much access to your database you are comfortable giving.
Query writers you paste a schema into. Text2SQL.ai generates SQL from plain-language requests using the schema you add, explains and fixes existing SQL, and has a desktop app that keeps database credentials on your machine. AI2SQL covers engines such as MySQL, PostgreSQL, SQL Server, Oracle, SQLite, Snowflake and BigQuery, and explains queries and fixes syntax errors.
Tools that connect to your database. AI for Database connects to sources such as PostgreSQL, MySQL, MongoDB and SQL Server with read-only access, and shows the generated SQL so you can inspect it. BlazeSQL reads your schema, writes and runs SQL from a chat, and returns charts and dashboards. Defog.ai is a data analyst aimed at companies, built on its open-source SQLCoder model.
Help inside your database IDE. dbForge AI Assistant is an add-on for Devart's dbForge database tools. It generates SQL from an attached database, rewrites slow statements, explains queries clause by clause and flags errors with suggested fixes.
Connecting a coding assistant to a database. DBHub is an open-source MCP server that lets assistants such as Claude, Cursor and VS Code explore schemas and run SQL. It offers a read-only mode, row limits and query timeouts, but you install and configure it yourself.
Step 1: Write the question precisely
Vague questions give vague SQL, so spend a minute on the wording.
- State the result you want: a single number, a list of rows, or a grouped table.
- Name the time range and any filters, such as "orders placed in the last full calendar month".
- Say how you define business terms like "active customer" or "revenue".
- Note the columns you expect in the output.
Check it worked by asking whether a colleague could write the same query from your wording alone.
Step 2: Prepare your schema
The tool can only use table and column names it knows about.
- Export the structure of the relevant tables, with columns, types, primary keys and foreign keys. Leave out the data itself.
- Trim it to the tables the question needs, so the tool is not distracted.
- Add short comments for confusing columns, such as what a status code means.
- Check whether your tool takes a pasted schema (Text2SQL.ai does) or reads it from a connected database (BlazeSQL and AI for Database do).
Check it worked by reading the schema back and confirming every table in your question appears in it.
Step 3: Choose a tool and set it up safely
You end this step with a tool ready, and with the least access it needs.
- If you only want drafts, choose a query writer and paste the schema.
- If you want answers directly, connect a database using a read-only user. AI for Database uses read-only access by default, and AI2SQL queries run read-only once you connect a database on Pro or Team.
- Select your database engine so the tool uses the right syntax.
- Check the plan limits in the comparison table below before you rely on a free tier.
Check it worked by running a trivial request, such as listing the first few rows of one table, and getting the right columns.
Step 4: Generate the query and read it
Never run SQL you have not read.
- Paste your question from Step 1 and generate the query.
- Read the joins first: are the right tables linked on the right keys?
- Check the filters, the date range and the grouping against your question.
- Ask the tool to explain the query clause by clause. dbForge AI Assistant and AI2SQL both list explanations as a feature.
Check it worked by being able to restate in words what each part of the query does.
Step 5: Test it on safe data
A query can return plausible numbers and still be wrong.
- Run it on a test copy, a replica or a read-only connection.
- Add a row limit while you test, so a mistake does not scan or return everything.
- Work out the answer a second way for a small case, such as counting a handful of rows by hand.
- Compare totals with a number you already trust, such as a dashboard figure.
Check it worked by getting the same answer from two independent routes.
Step 6: Fix, optimize and explain
Iterate on the query rather than starting over.
- If the query errors, paste the error message back and ask for a fix. Several tools list syntax-error fixing.
- If it is slow, ask for an optimized version, then compare the results with the original so they still match. AI2SQL lists optimization on Pro and Team, and dbForge AI Assistant rewrites slow statements.
- Ask for comments in the SQL so the next person can follow it.
- Check any rewritten query on the same safe data again.
Check it worked by confirming the optimized query returns exactly the same rows as the original.
Step 7: Save, share and set guardrails
Make the query repeatable without making the data riskier.
- Save the final SQL in version control or your team's query library, with the question beside it.
- Keep database users read-only for AI tools, and set row limits and timeouts where the tool supports them. DBHub, for example, lists a read-only mode, row limits and query timeouts.
- Note who may run the query and how often.
- Re-check the query when the schema changes.
Check it worked by having a colleague re-run the saved query and get the same result.
Tools for this job compared
| Tool | Good fit for | Pricing | Stated limits |
|---|---|---|---|
| AI2SQL | Plain-English to SQL for many engines, with explain and fix | Freemium, paid from $9/mo (checked 24 Oct 2026) | Free plan: 3 SQL queries a day on a demo database; your own database needs Pro or Team |
| Text2SQL.ai | Schema-aware query writing, with a desktop app | Free trial, paid from $29/mo (checked 24 Oct 2026) | API access is on Pro and above and uses plan credits; the site's pricing page shows an Under Construction notice |
| BlazeSQL | Chat with a SQL database and get charts and dashboards | Free trial, paid from $39/mo (checked 24 Oct 2026) | Paid subscription required after the trial; the AI sees table and column names |
| AI for Database | Questions on PostgreSQL, MySQL or Google Sheets, with dashboards | Freemium, paid from $20/mo (checked 24 Oct 2026) | AI usage pauses when credits run out; per-seat fee and individual credit balance |
| dbForge AI Assistant | SQL help inside dbForge database tools | Freemium | Works only inside dbForge products; Express license capped at 200,000 tokens per month |
| DBHub | Connecting Claude, Cursor or VS Code to a database | Open source | You install and run it yourself; needs Node.js 22.5 or newer, or Docker; write access possible unless you set readonly |
| Defog.ai | Company data analysis on Snowflake, Postgres and more | Contact sales, from $5,000/mo (checked 24 Oct 2026) | Published pricing starts at $5,000/month on the Enterprise cloud plan; self-hosted use needs an annual commitment |
Prices and plan limits change, so confirm the current terms on each vendor's site before you subscribe.
Common mistakes to avoid
- Running generated SQL on production. A bad join or an unintended write can cost real time. Fix it by using a read-only user and a test copy first.
- Skipping the schema. Without table and column names, the tool guesses, and guesses look convincing. Fix it by always supplying the relevant structure.
- Trusting the first numbers. A query that runs is not a query that is right. Fix it by checking a small case by hand and comparing with a figure you already trust.
- Sharing sensitive records. Pasting customer data into a hosted tool may breach your policy. Fix it by sharing the structure only, and by reading the vendor's data terms. BlazeSQL, for example, notes the AI sees table and column names even in its desktop version.
- Leaving ambiguous definitions. Terms like "active user" mean different things in different teams. Fix it by writing the definition into the question or into the tool's notes.
How we put this together
This is an evidence-based guide built from each tool's listing in our directory, which is compiled from the vendor's own site and pricing page. It is not a review based on using the tools. We don't rank tools, and nobody paid to be included. Some outbound links may be affiliate links; see our disclosure.
Frequently asked questions
Is there a free way to write SQL queries with AI?
Yes, with limits. AI2SQL's Free plan covers 3 SQL queries a day on a demo database, dbForge AI Assistant has a free Express license with 200,000 tokens a month, and DBHub is open source with no paid plans listed. Chat assistants may also write SQL if you paste your schema.
Can AI write accurate SQL?
It can write correct queries for clear questions and a well-described schema, but it can also produce queries that run and return wrong numbers. Read the SQL, test it on safe data and verify any figure used for finance, reporting or decisions.
Does the AI need access to my database?
Not always. Query writers such as Text2SQL.ai work from a schema you provide. Others connect to your database, and then you should use a read-only account and confirm the vendor's data handling.
Which databases are supported?
It depends on the tool. AI2SQL lists MySQL, PostgreSQL, SQL Server, Oracle, SQLite, Snowflake and BigQuery. DBHub supports PostgreSQL, MySQL, MariaDB, SQL Server, Oracle and SQLite. Check the listing for your engine before you commit.
How long does it take to get a working query?
The vendors do not publish typical times. Preparing the schema, reading the query and testing it usually take longer than generating the draft.
Compare more options in our Coding category. If you are documenting what you build, see how to document a codebase with AI.