SQL Query Prompt Generator
Build a SQL prompt with real table structure and dialect-specific rules, so the query and its explanation are usable on the first try.
Getting a model to write correct SQL depends on giving it the actual table structure, not just a description of what you want the query to do — without real column names and types, the model has to guess at schema details it will often get wrong. This tool builds a prompt that includes your actual schema alongside the query request.
How to use it
Table and column names, plus types if relevant -- the real schema, not a description of it.
The actual question you're trying to answer with the data.
Includes both your real schema and the query request together.
Tips for better results
- Include actual column names, not paraphrased descriptions. A model guessing at column names based on a description will often guess wrong.
- Specify your SQL dialect if it matters. Different databases have real syntax differences -- stating the dialect avoids a query needing manual correction.
- Always review generated queries before running them against real data. Especially anything involving deletes or updates -- treat generated SQL as a draft to verify.
Example output
“Schema: orders(id, customer_id, total, created_at). Request: total revenue per month for 2025. Produces a query using the actual column names, not guessed ones.”
TL;DR
Asking an AI model to “write a SQL query” without describing the actual tables produces a query built on guessed column names and a generic dialect that may not match what you are actually running. This tool builds a prompt around your real table structure, the specific SQL dialect you use, and any performance constraints that matter — the details that turn a plausible-looking query into one that actually runs against your database on the first try.
Why dialect and schema details matter more than the query logic
Most SQL dialects agree on the core logic of a SELECT with a JOIN and a WHERE clause, but differ meaningfully on window functions, date handling, string concatenation, and pagination syntax — differences that turn a technically correct query into one that fails immediately when run. Providing the actual dialect up front means the model writes syntax that will actually execute, rather than a generic ANSI-SQL approximation that needs manual translation afterward.
This matters especially for anyone working across multiple database systems in the same week, where it is easy to accidentally write MySQL-flavored LIMIT syntax into a SQL Server query expecting TOP, or forget that PostgreSQL and MySQL handle string concatenation with entirely different operators. Stating the dialect explicitly in the prompt removes that entire class of easy-to-miss mistake before the query is even written.
Describing a top-10-customers query against an orders and customers table, PostgreSQL dialect, produces:
“Write a PostgreSQL query that does the following: find the top 10 customers by total order value in the last 90 days.
Tables and relevant columns:
– orders(id, customer_id, total, created_at)
– customers(id, name, email)
Constraints:
– avoid correlated subqueries if possible
– created_at is indexed, prefer filtering on it directly…”
That gives the model the real schema and a real performance constraint, instead of guessing at both.
Using this across ChatGPT, Claude, and Gemini
All three models handle SQL well when given real schema details, but the plain-language explanation requested at the end of the prompt varies in usefulness — Claude tends to flag potential performance issues (like a missing index) most proactively without being asked twice, which is useful when reviewing a query before running it against a production database rather than a test environment.
FAQ
Will this write queries that are guaranteed to be fast?
No — it produces a reasonable, well-structured first attempt and flags likely performance issues along the way, but actual real-world query performance depends heavily on your specific data volume, existing indexes, and database configuration, all of which should be tested directly rather than assumed.
What if I do not know all the exact column names?
Describe them as closely as you reasonably can — the model will still produce a usable structure and reasonable guesses either way, though you will likely need to correct a few exact names before actually running it against your real database.
Can I use this for something other than SELECT queries?
Yes — describe the goal as an insert, update, or delete operation instead of a lookup, and the same schema-and-dialect approach still applies fully, though extra care is always warranted before running any generated write operation against real, live data.
Should I always run the generated query on a test database first?
Yes, especially for anything beyond a simple SELECT — treat AI-generated SQL the same way you would treat SQL written by a new team member on their first week, worth a careful review before it ever touches a production system.