SQL Query Writer Prompt
What this prompt does
This prompt turns a question about your data into a working SQL query. It is designed for people who know what they want to find out — “which customers spent the most last month?” — but are not sure how to write the query. The prompt asks the model to write the query, explain it in plain English, and say what each part does, so you can learn from it as well as use it.
The prompt
Act as a careful database developer who writes clear SQL queries and explains them.
Write a SQL query that answers this question: [QUESTION, e.g. "Which 10 customers spent the most in the last 30 days?"]
Database: [DATABASE, e.g. "PostgreSQL" or "MySQL"]
Tables and columns:
[DESCRIBE YOUR TABLES HERE, e.g. "customers (id, name, email), orders (id, customer_id, total, created_at) — orders.customer_id links to customers.id"]
Give me:
1. The query: in a code block, on its own, so I can copy it.
2. Explanation: a short paragraph saying what the query does overall, then a line-by-line explanation of each part in plain English.
3. What to check: 2-3 things I should verify before running it, such as the date range or the table names.
4. A safer version (if the query changes data): if my question involves UPDATE or DELETE, show the SELECT version first so I can check which rows it would affect.
Rules:
- Use only the table and column names I gave you. Do not invent tables or columns.
- Write a read-only SELECT query unless I explicitly asked for a change. If I asked for a change, warn me to back up the data first.
- Keep the query as simple as it can be while still answering my question.
Which model
Written for both ChatGPT and Claude. Paste it as one message, including your table structure. Claude often writes careful, well-explained SQL; ChatGPT is also strong, especially for common queries.
How to use it
- Copy the prompt into ChatGPT or Claude as one message.
- Write your question in plain words: what do you want to find out? One question per prompt.
- Describe your tables and columns, including how they link together. This is the most important part — without it, the query will guess your table names.
- Add your database type, then send it.
- Read the explanation, check the table and column names, then run the query on test data first. Check the result against a number you already know.
Example
Input: Question: “Which 10 customers spent the most in the last 30 days?” Database: PostgreSQL. Tables: customers (id, name, email), orders (id, customer_id, total, created_at).
Tips
-
- Describe your tables every time. The model cannot see your database, so it can only use what you tell it.
- Ask for a SELECT version first, even when you want to change data. Checking which rows a change would affect is the safest habit in SQL.