Write SQL from a plain business question with Claude
The struggle: You know exactly what you want answered, but you cannot write the joins, and the analyst queue takes days.
What you end up with: A tested SQL query for your question, with a plain-English check of what it counts.
Give Claude your table names and columns, not the data, and state the question with its definitions. Then make it explain the query and test it on a small sample before you trust the number.
Tools: Claude. Level: Free to start. Time: 30 min.
Use it if
- You know your tables but not the joins
- You get the same data question every week
- You can run a query on a copy or read-only replica
Skip it if
- You would have to paste customer personal data into the chat
- You want Claude to run queries on live production data
What you need first
- A Claude account (the free plan works)
- Your table names, column names and how tables join, copied from the schema, with no real data
The method
Share the schema, not the data (Claude: Chat)
Copy the table and column names, types and keys from your database tool. Paste them into a new chat with a line on what each table holds. Leave out all rows.
You get: Claude knows the structure and sees no customer data.
Tip: Say which SQL dialect you use. Date functions differ in every one.
Ask the question with definitions (Claude: Chat)
Write the question and define the words that carry meaning, such as active, revenue or churned. Send the prompt and read the assumptions before you read the query.
Prompt to copy:
You are a careful analyst. My database is [dialect]. Schema: [paste schema]. Question: [question]. Definitions: [define each term]. Write one read-only SELECT query. Before the query, list your assumptions. After it, explain each join and filter in plain English. If the schema cannot answer the question, say what is missing instead of guessing.
You get: A query plus the assumptions behind it.
Tip: If a definition is missing, ask Claude to ask you. Do not accept its guess.
Test it on a small sample (Claude: Chat)
Add a date limit or LIMIT clause and run it on a copy or a small slice. Paste the result and the row count back, and ask Claude whether it looks right.
Prompt to copy:
Here is the result of the query on a small sample: [paste first rows and row count]. Check it for signs of duplicated rows from joins, missing NULL handling, wrong date ranges or counting the wrong thing. If anything looks off, show the fixed query and explain why.
You get: A query that works on a small case.
Tip: Work out one row of the answer by hand. If it does not match, the join is wrong.
Make it safe and reusable (Claude: Chat)
Ask for the final version with comments, no write statements, and parameters for dates. Save it in your team's query library yourself.
Prompt to copy:
Rewrite the final query with a comment above each block, date ranges as [start_date] and [end_date] placeholders, and only SELECT statements. Then list two ways the answer could mislead a reader, for example duplicates or late data.
You get: A commented query anyone can rerun.
Tip: Ask Claude to list two ways the number could be wrong. Keep that note with the query.
Where this goes wrong
- A query can run and still count the wrong thing. Check one figure by hand.
- Run generated SQL read-only first and check the results. Never run generated writes or deletes on production without review, and never on live data.
- A data owner should approve any number that goes into a board or customer report.
Sources
More like this
- An incident timeline rebuilt from your git history
- Agent pull requests checked before a human merges
- A tracking plan that cleans up your event names
Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.