SQL from English Prompt — Write Queries Without the Syntax Tax
Asking an AI to write SQL is the difference between ‘translate this English to SQL’ (often wrong, always overconfident) and ‘translate this English to SQL, but ask me if anything is ambiguous’ (usually right, calls out assumptions). This prompt is the second version. It writes cleaner queries and tells you when the question was unclear.
The template
Replace the [bracketed] fields with your specifics. Send to any AI chat.
I have a [Postgres / MySQL / Snowflake / BigQuery] database with these tables: [paste schema or describe tables and columns] I want to know: [your question in English] Write the SQL query. Constraints: - Use CTEs over nested subqueries (readability) - Add a 1-line comment explaining each CTE - If my question is ambiguous, ASK before writing the query — don't guess - Explicitly handle NULLs in any aggregation - Show me roughly how slow this might be on a 100M-row table
How to use it
- Replace `[Postgres / MySQL / Snowflake / BigQuery]` with your actual dialect — SQL flavors differ in subtle ways (window functions, CTE behavior, NULL ordering) that matter for correctness.
- Paste your table schemas (or describe them in English). The more accurate your schema description, the more correct the query.
- Read the AI’s clarifying question carefully — it’s often surfacing an ambiguity you didn’t realize was in your own question.
Example output
Here’s an example of what a quality AI returns when you run this template:
Why this prompt works
Forcing dialect specification prevents subtle bugs.
MySQL doesn’t have FULL OUTER JOIN. Snowflake’s DATE_TRUNC syntax differs from Postgres. BigQuery uses STRUCT differently. Naming the dialect upfront prevents queries that look right but fail.
‘Ask before guessing’ eliminates half of wrong queries.
Most natural-language SQL requests are ambiguous in ways the asker doesn’t notice. Forcing a clarifying question surfaces those ambiguities cheaply.
CTEs over nested subqueries is a readability win.
Models default to nested subqueries because they’re slightly fewer characters. CTEs are easier to read, easier to debug, and roughly the same performance in most query planners.
‘How slow on 100M rows’ surfaces missing indexes.
Without this, the model generates correct queries that would crash production. Including it forces the model to consider scan-vs-index access patterns.
Which AI to use
Read the full comparison in ChatGPT vs Claude vs Gemini in 2026 →
Related prompts
Code review prompt →After you have the SQL, review it for security and edge cases.Debug code prompt →When a query returns wrong results, not just slow. All 27 prompt templates + free generator → Pick a category, fill in the blanks, copy your prompt.Frequently asked questions
Should I trust AI-written SQL on production data?
Test on a copy first. AI gets simple queries right ~95% of the time, but the 5% includes subtle bugs (off-by-one date ranges, wrong join type, missing GROUP BY) that aren’t visible until you compare against expected output.
Can I include my actual data in the prompt?
Don’t paste real user data, real emails, real PII. Either anonymize first or use enterprise tiers that don’t train on inputs. For schema-only or anonymized samples, consumer tiers are fine.
What about really complex queries — window functions, recursive CTEs?
AI handles these but verifies less reliably. For complex analytical SQL, treat AI as a starting point you’ll significantly modify, not a finished query.
Will it know my company’s specific tables?
Only if you describe them. Always paste the schema (table + column names + types) for accurate results. Naming conventions vary so much that guessing fails often.
Can I ask the AI to optimize my existing slow query?
Yes — paste the query and the EXPLAIN output, ask for optimization. Be explicit about your goal (faster reads vs. lower memory vs. better lock behavior).
Build any prompt in 30 seconds
Free tool, no signup, runs in your browser. Pick a category, fill in the blanks, copy your prompt.
Try the prompt generator → Read the full guide