Prompt

SQL Query Writer

sqldatabasedata

SQL Query Writer: From Plain English to Working SQL

Writing SQL means holding the whole schema in your head while juggling JOINs, GROUP BY, and edge cases. This AI tool flips that around: describe the question in plain English, hand over your schema, and get back a query that actually runs.

Full Prompt
Translate a plain-English data question into a correct, efficient SQL query, with a short explanation of how it works.

QUERY-WRITING METHODOLOGY (follow in order):

1. Restate the Question
   Goal: Confirm you understood the request before writing SQL.
   - In one sentence, restate what the query should return.
   - List the tables and columns you will use, based on the schema provided.
   - If the schema is missing a needed table or column, state the assumption you are making.

2. Build the Query
   - Use standard ANSI SQL unless a specific dialect is given (Postgres, MySQL, SQLite, BigQuery, SQL Server).
   - Prefer explicit JOINs over implicit comma joins.
   - Alias tables with short, meaningful names.
   - Filter in WHERE, aggregate with GROUP BY, and filter aggregates with HAVING.
   - Add ORDER BY and LIMIT when the question implies ranking or a top-N result.
   - Format the query across multiple lines with consistent indentation.

3. Explain and Verify
   - Walk through the query clause by clause in 3-5 short bullets.
   - Note any edge cases the query handles or ignores (NULLs, duplicates, ties).
   - If a more performant version exists for large tables, mention it briefly.

OUTPUT CONSTRAINTS:
- Return the query in a single code block, ready to run.
- Use the exact table and column names from the provided schema.
- Do not invent columns that were not provided.
- If the request is ambiguous, return your best query plus one clarifying note.
- Keep the explanation under 120 words.

---

MY INFO:

Database Dialect (required): [PostgreSQL / MySQL / SQLite / BigQuery / SQL Server]

Schema (required): [paste table names and columns, or a CREATE TABLE statement]

Question (required): [what do you want the query to return?]

Performance Notes (optional): [table sizes, indexes, anything relevant]

What You Get

  • A ready-to-run query formatted across multiple lines for readability
  • Correct JOINs using explicit syntax and meaningful aliases
  • A clause-by-clause explanation so you understand and trust the result
  • Edge-case notes covering NULLs, duplicates, and ties

Why It Works

Most SQL mistakes come from ambiguity, not syntax. The prompt forces a restatement of your question and an explicit list of the tables and columns it will use before any SQL is written. That single step catches wrong assumptions early — the difference between a query that looks right and one that returns the right rows.

Best Practices

  1. Paste a real schema: Provide table names and columns, or a CREATE TABLE statement, so column names match exactly.
  2. Name your dialect: PostgreSQL, MySQL, BigQuery, and SQL Server differ in functions and syntax.
  3. Verify on a sample: Run the query against a small dataset before trusting it on production tables.
  4. Mention table sizes: For large tables, you will get a more performant variant.

Frequently Asked Questions

Q: Which databases does it support? A: Any SQL dialect — specify PostgreSQL, MySQL, SQLite, BigQuery, or SQL Server and the syntax adapts.

Q: Can it handle complex joins and aggregations? A: Yes. It handles multi-table JOINs, GROUP BY with HAVING, window functions, and subqueries, and explains each part.

Stop wrestling with syntax and start asking your data questions in plain English.