SQL from a question, tested before you trust it with ChatGPT
The struggle: You know the question you need answered but not the joins, and a wrong query returns numbers that look right.
What you end up with: A SQL query for your question, explained line by line, and checked against a small sample before it touches real data.
ChatGPT writes the query from your schema and explains each join. You then check the logic on a tiny CSV sample with Data analysis, so a silent mistake shows up before anyone uses the number.
Tools: ChatGPT. Level: Free to start. Time: 30 min.
Use it if
- You get a schema and joins confuse you every time
- You need a number from a database and analysts are busy
- You can run the query yourself on a copy or read-only replica
Skip it if
- The data is regulated or personal and you cannot paste the schema
- You would run the query on a live production database without a test
What you need first
- A ChatGPT account
- Table and column names only, no real rows
- A copy or read-only access to run the final query
The method
Explain the schema (ChatGPT: Chat)
Paste table names, column names, types and the keys that link tables. No rows of real data. Ask ChatGPT to describe how the tables relate before any query is written.
Prompt to copy:
Here is a database schema, tables and columns only: [paste schema]. Explain in plain English how the tables relate, which columns are keys, and which joins are safe. List any column whose meaning you are unsure of and ask me before assuming. My database is [PostgreSQL, MySQL, SQL Server or other].
You get: A plain-language map of your tables.
Tip: If its map is wrong, fix the schema now. Every later query inherits the mistake.
Write the query (ChatGPT: Chat)
Describe the question in business words, including the date range, what counts as a row and what to exclude. Ask for the query plus a line by line explanation.
Prompt to copy:
Write a [dialect] query for this question: [question]. One row in the result should be [what a row means]. Use only the tables I gave you. Explain each join and filter in one line each. Say which numbers could be double counted by a join and how you avoided it. If the question is ambiguous, ask me first.
You get: A query and an explanation you can read.
Tip: State what one row of the result means. It stops accidental double counting.
Build a tiny test set (ChatGPT: File uploads)
Export 20 to 50 made-up or anonymised rows per table to CSV, or ask ChatGPT to invent sample rows that include the tricky cases: duplicates, nulls and a customer with no orders. Upload them.
Prompt to copy:
Create sample CSV data for these tables: [schema]. 30 rows per table. Include duplicates, nulls, a parent row with no child rows and two rows that your query should exclude. Tell me which rows are the traps.
You get: A small dataset where you know the right answer.
Tip: Include rows that should be excluded. A test with only good rows proves nothing.
Check the logic on the sample (ChatGPT: Data analysis)
Upload the sample CSVs and ask ChatGPT to run the same logic with Data analysis. Compare its answer to the answer you worked out by hand for the sample.
Prompt to copy:
Using the uploaded sample files, calculate the result of this query logic: [paste query]. Show the result table, then list every sample row that was excluded and why. I expect [your hand-worked answer]. If the result differs, find which join or filter causes it.
You get: Proof the query logic gives the expected answer.
Tip: Data analysis recreates the logic in code. It is not your database, so still run the SQL on a copy.
Review for safety and speed (ChatGPT: Chat)
Ask ChatGPT to review the final query for write statements, missing limits and slow patterns. Run it yourself on a copy or read-only access and compare the total with a number you already trust.
Prompt to copy:
Review this query: [query]. Confirm it only reads data. Point out anything that could be slow on a large table, such as functions on indexed columns or missing date filters, and suggest a safer version. Do not change the meaning.
You get: A reviewed query and a matching total.
Tip: Run the totals against a known report first. If they match, the query is probably right.
Where this goes wrong
- Never run a query you do not understand on a live database, and never run anything that writes or deletes without a developer checking it.
- Do not paste real rows, customer names or credentials. Schema names are enough.
- ChatGPT does not know your data's quirks, so a matching sample does not prove the full result is right.
Sources
- ChatGPT Help Center: Data analysis with ChatGPT
- ChatGPT Help Center: Uploading files and audio to ChatGPT
More like this
- Agent pull requests checked before a human merges
- An incident timeline rebuilt from your git history
- Acceptance criteria written for vague tickets
Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.