SQL from English Prompt — Write Queries Without the Syntax Tax

Coding prompt

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.

✓ Works in ChatGPT, Claude, Gemini ✓ Tested template ✓ Copy + paste ready

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

  1. 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.
  2. Paste your table schemas (or describe them in English). The more accurate your schema description, the more correct the query.
  3. 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:

Before I write this — quick clarifying question: When you say “users who haven’t logged in this month,” do you mean: – (A) Users with no `last_login_at` value at all (never logged in) – (B) Users whose `last_login_at` is null OR before the start of this calendar month – (C) Users whose most recent login from the `login_events` table is before the start of this month I’ll assume (B) unless you say otherwise. “`sql — Get inactive users this month + their account info WITH active_users AS ( — All non-deleted users with a non-null created_at (sanity filter) SELECT user_id, email, created_at, last_login_at FROM users WHERE deleted_at IS NULL ), inactive_this_month AS ( — Users whose last login was null OR before this month started SELECT user_id, email, created_at, last_login_at FROM active_users WHERE last_login_at IS NULL OR last_login_at < DATE_TRUNC('month', CURRENT_DATE) ) SELECT * FROM inactive_this_month ORDER BY created_at DESC; ``` **Performance:** On a 100M-row `users` table, this would do a full scan unless you have an index on `last_login_at`. Add `CREATE INDEX idx_users_last_login ON users(last_login_at);` for sub-second performance.

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

**Claude** for query structure and clarifying questions. **ChatGPT** if you need to run the query against sample data (its Python interpreter can simulate small tables). **Gemini** for BigQuery specifically — it has the most BigQuery training data of the three.

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