Prompt to write a SQL query from plain English
Describe the question you want answered and paste your schema to get a correct SQL query for your database, with each join and filter explained.
- Category
- Coding
- Tags
- Data and SQL
- Added
- Added
Fill in the prompt
The prompt below changes as you type, and Copy prompt copies it filled in. What you type stays in this browser. Leave a box empty to fill it in after you paste.
The prompt
Write a SQL query for {{database}} that answers this question:
{{question}}
The schema:
{{schema}}
Rules:
- Use only the tables and columns in the schema. If something you need is missing, say so instead of inventing it.
- State every assumption about the data, such as whether a column can be null or a join can produce duplicate rows.
- Prefer clear joins and CTEs over nested subqueries.
- After the query, explain each join and filter in one line.
- If the query could be slow on a large table, say which index would help.
Then show a few example rows that would appear in the result, so I can sanity-check it.
Replace each {{highlighted part}} with your own text before you send it.
How to use it
Name your database and its version in {{database}}, like "PostgreSQL 16" or "SQL Server 2022". Date functions, string handling and how you limit rows all differ between them, and a query in the wrong dialect fails in ways that are tedious to untangle.
Paste the CREATE TABLE statements for the tables involved into {{schema}}, not the whole database. Write {{question}} the way you'd ask a colleague, with the time range and a definition of anything that could be counted two ways.
Example for {{question}}: "Monthly active customers for 2026, where active means at least one paid order in that month."
Read the stated assumptions before you run anything against production. That's where a duplicated row from a one-to-many join usually shows up. The sample result rows at the end give you something to compare against a query you already trust.