AI SQL Query Generator: how it works and how to build one
Turn a question into SQL for Postgres, MySQL, SQLite and more. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Ask in plain English, for example ‘top 10 customers by revenue last month’, and get a query in your database’s dialect. Paste your CREATE TABLE statements first and it will use your real table and column names.
When to use it
- Reports for people who don’t know SQL
- Learning joins and GROUP BY
- Drafting migrations
- Checking an approach before you write it
How to use it
- Log in.
- Paste your table definitions (optional), then your question.
- Copy the query and run it on a test database first.
Try it free with an Eskillsfy account: https://eskillsfy.com/tools/ai-sql-generator
How it works
Text-to-SQL works best when the model knows your schema. Pasting CREATE TABLE statements gives it the real table and column names, so it doesn’t guess.
The dialect matters for dates, limits and string functions, so it is part of the prompt. The tool only writes SQL; it never connects to a database.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any Node.js project.
server/routes/ai-sql-generator.js (js)
// Express route. The prompt stays on the server, so the endpoint can only do this one job.
import { Router } from "express";
const router = Router();
const SYSTEM = (option) => `Write a ${option} query for what the user asks. Use table names from any CREATE TABLE they include. Return SQL in a code block, then explain.`;
router.post("/ai-sql-generator", requireUser, async (req, res) => {
const input = String(req.body.input ?? "").trim();
if (!input || input.length > 8000) return res.status(400).json({ error: "Send 1 to 8,000 characters." });
const option = ALLOWED.includes(req.body.option) ? req.body.option : ALLOWED[0];
// Any chat model works here: Gemini on Vertex AI, the Claude API, etc.
const r = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: { "x-api-key": process.env.ANTHROPIC_API_KEY, "anthropic-version": "2023-06-01", "content-type": "application/json" },
body: JSON.stringify({ model: process.env.AI_MODEL, max_tokens: 2048, system: SYSTEM(option), messages: [{ role: "user", content: input }] }),
});
if (!r.ok) return res.status(502).json({ error: "The AI service did not answer." });
const data = await r.json();
res.json({ output: data.content.map((b) => b.text ?? "").join("").trim() });
});
export default router;
Make it production-ready
- Put the API key in a secret manager and read it from an environment variable, never in frontend code.
- Require a login and add a per-user daily limit so one person can’t run up your bill.
- Validate the option against a fixed list so users can’t inject their own instructions.
- Show a loading state and a clear error when the provider is slow or down.
- If you ever run generated SQL, use a read-only database user and a statement timeout.
Questions
Can it run the query?
No. It writes SQL only. Run it yourself, and check UPDATE or DELETE queries carefully.
Free with an Eskillsfy account.