Using ChatGPT to write SQL for your reports: six steps and a check
You can get a working SQL query from ChatGPT or any AI assistant by pasting your schema, naming your dialect and stating the definition in the question. What our own test showed is that the query will run and still be wrong when it guesses a value that is not in the schema. The check that caught every wrong answer needs no SQL: does the total match a number you already trust. Results are labelled for ChatGPT (free plan) and Claude separately.
You can ask an AI assistant for the SQL behind a report and get something that runs. The part that needs your attention is not the syntax, it is the number, before it goes to a client or your boss: in our test every query ran, and some of them quietly returned nothing.
Originally published in April 2024. Fully updated in October 2026 with a tool-agnostic workflow, a test on a demo warehouse and a check that catches wrong queries.
This page is for someone who owns a report, knows a little SQL and cannot tell by reading a long query whether it is right. That is fine: the check below does not need you to read the SQL.
To forward to a boss: an AI assistant can write the SQL for a report, but it can also write a query that runs and shows zeros, so every figure should be compared with a number the company already trusts before anyone acts on it.
We ran the same six business questions on ChatGPT (free plan) and on Claude, and labelled every result with the model. The results do not transfer to other models or plans. The details are in how we tested.
Reading to get this done?
The short answer
Give the assistant three things: your schema (the list of your tables and their columns), your SQL dialect (which database’s version of SQL you use) and the definition of the figure. Then compare its total with a number you already trust.
That is the whole method. In our 24 plain runs, all 24 queries passed BigQuery’s dry run (a check that a query is valid, without running it) and all 24 ran. Five returned a wrong answer without any error, and the same check caught every one: the total did not match a figure we already knew. The cause was a single misspelled value. The likely fix, giving the assistant the allowed values, was not tested.
Six steps to a first query
These steps work in ChatGPT, Claude or any other assistant. None needs a connection to your data: you paste text and get text back.
Step 1: Choose your assistant
The free ChatGPT plan and Claude each wrote queries that ran in all 12 of our runs. We did not test paid plans, other models or features that let an assistant run code.
Step 2: Paste your schema as text
The assistant cannot see your data. It only knows the tables and columns you tell it about. The schema is that list: table names, column names and the type of each. If you do not have it, ask whoever owns the data.
Here is a line of the kind of schema we pasted in our test:
orders: session_id STRING, customer_id INT64, order_date DATE, order_id STRING, status STRINGAdd the allowed values of any column you will filter on. Our schema listed the column status but not the values in it. That gap caused every wrong answer in the test, as the worked example shows. To get the values, run SELECT DISTINCT status FROM your_table, or ask whoever owns the data. In our demo shop they are Completed, Cancelled and Returned. We did not test whether pasting them fixes the problem; they are what the assistant was missing.
Step 3: Name your dialect
Say which database the query is for, in the first line: “Google BigQuery (GoogleSQL)”, “Snowflake”, “SQL Server”. Each speaks its own version of SQL, called a dialect. This one is cheap to get right, because the mistakes are loud. A short example is below.
Step 4: Put the definition in the question
Write the question the way you would explain the figure to a colleague. Our fourth question ended: “Use net revenue of completed orders, attributed by the session’s source, over ad spend.” Both models used that definition.
Step 5: Read the assumptions, not the SQL
Below the query, the assistant usually lists what it assumed. Read that list as if it were the brief. It is plain language, and it is where the wrong answers announced themselves. All 12 Claude replies and nine of 12 ChatGPT replies stated an assumption. In four of the five wrong answers, the notes said it was assuming the spelling of the status value; the fifth did not say it was guessing.
Here is what that looks like, word for word from one ChatGPT reply (free plan, 14 October 2026). The prompt ended with that question. The query it returned contained the line WHERE o.status = 'completed', and the note below the query said: “This query assumes orders.status uses the exact value ‘completed’.” The value in the data is Completed. The sentence was there. It was easy to read past.
Step 6: Run it, then check the total
Run the query. Then do the check in the next section before the figure goes into a report. A query that runs, or passes a dry run, has told you only that it is valid. In our test the five wrong queries passed the dry run just as the right ones did.
ChatGPT writes SQL that runs, and you can't tell whether the number it returns is right before it goes to your boss.
Paste this into your AI assistant. It reads your actual schema, writes the report query, states its assumptions and checks the total against a number you already trust.
The prompt
Read https://www.owox.com/ai-agent-setup.md and follow it to set up OWOX Data Marts for me, starting from /blog/articles/how-to-build-sql-queries-with-chatgpt.md. The task: write me the SQL for return on ad spend by source against my own schema, list every assumption you made about values, and check the total against my ad platform's spend. Tag the links you give me with utm_campaign=ai-task and utm_term=b1. Use what you know about my context and goals
A worked example: return on ad spend by source
A worked example: return on ad spend by source
This is the query Claude wrote for question four, from the schema above and nothing else. It is a good query: ad spend and revenue are each added up per source before they are joined, so neither is counted twice. We made one fix, 'Completed' with a capital C in the status line, and two cosmetic changes: we wrapped the last columns in ROUND(..., 2) and removed Claude’s comments. We explain the fix after the results.
WITH order_revenue AS (
SELECT o.order_id, o.session_id,
SUM(p.line_net_revenue) AS net_revenue
FROM `owox-demo.ecommerce.orders` AS o
JOIN `owox-demo.ecommerce.purchases` AS p
ON p.order_id = o.order_id
WHERE o.status = 'Completed'
AND o.order_date BETWEEN DATE '2026-07-01' AND DATE '2026-09-30'
GROUP BY o.order_id, o.session_id
),
session_source AS (
SELECT session_id, ANY_VALUE(LOWER(TRIM(source))) AS source
FROM `owox-demo.ecommerce.sessions`
GROUP BY session_id
),
revenue_by_source AS (
SELECT s.source, SUM(r.net_revenue) AS net_revenue
FROM order_revenue AS r
JOIN session_source AS s ON s.session_id = r.session_id
GROUP BY s.source
),
spend_by_source AS (
SELECT LOWER(TRIM(source)) AS source, SUM(spend) AS ad_spend
FROM `owox-demo.ecommerce.ad_performance`
WHERE date BETWEEN DATE '2026-07-01' AND DATE '2026-09-30'
GROUP BY source
)
SELECT COALESCE(sp.source, rv.source) AS source,
ROUND(sp.ad_spend, 2) AS ad_spend,
ROUND(rv.net_revenue, 2) AS net_revenue,
ROUND(SAFE_DIVIDE(rv.net_revenue, sp.ad_spend), 2) AS roas
FROM spend_by_source AS sp
FULL OUTER JOIN revenue_by_source AS rv ON rv.source = sp.source
ORDER BY roas DESCRun against our demo shop on 14 October 2026, it returns:
| source | ad_spend | net_revenue | roas |
|---|---|---|---|
| 8,649.60 | 258,002.23 | 29.83 | |
| tiktok | 10,050.89 | 48,023.28 | 4.78 |
| 1,537.73 | 7,299.79 | 4.75 | |
| bing | 8,877.82 | 42,013.74 | 4.73 |
| 15,415.55 | 66,466.98 | 4.31 | |
| 4,111.81 | 2,058.83 | 0.50 | |
| 419.14 | 169.90 | 0.41 | |
| empty | 64,853.98 | empty | |
| direct | empty | 360,288.68 | empty |
Google’s 29.83 beside 4 to 5 for the others looks wrong, and it is not. It comes from the data. Our definition attributes revenue by the session’s source. For Google, 217,317.12 of the 258,002.23 (about 84%) comes from sessions whose medium is organic, and the spend we have is all on cpc. Counting only the cpc sessions, Google’s revenue is 40,685.11 and its ROAS is 4.70, in line with the others. The question was answered exactly as asked; it just compares all of Google’s revenue with only part of Google’s traffic. How to tell a real outlier from a wrong figure: take the one row that stands out and split it by one more column (here the medium), and check that the revenue and the spend are about the same thing. An outlier that survives is a finding, and one that disappears was a definition mismatch.
Now the version without our fix. Both assistants wrote the status as lowercase 'completed'. The real value in the data is 'Completed', so the filter matched no orders. Claude’s query returned the same seven sources with net_revenue and roas empty on every row. ChatGPT’s returned 0.0 for both, because it replaced a missing value with zero. No error appeared. The query ran, the sources were right, and the ad spend column was right. Both models had written, below the query, that they were assuming the spelling.
The check: does the total match a number you trust?
The check: does the total match a number you trust?
Add up the net revenue column of the report. In the example the total is 849,177.41; in the wrong versions it was empty or zero. You only need a figure from somewhere else: the shop’s own sales report, the finance figure for the same quarter, the ad platform’s report for spend. In our test that was the check that worked: the total reconciled with an independent figure caught five of five wrong answers. Be clear about what those five were: the loud kind, empty, zero or NULL, so any comparison with a real total would have shown them. The check does not prove a figure is right. A total can match while the split between channels is wrong, for example if revenue is credited to the wrong source. We did not have such a case in our 24 runs.
When does a total count as matching? When it uses the same definition and the same period as your trusted figure, and any difference is one you can explain, such as refunds, shipping or timing. We do not give a percentage tolerance, because the right one depends on the figure. A difference you cannot explain means the query or the comparison is wrong, and you should not use the figure yet. If two honest sources disagree for reasons you cannot explain, such as Shopify and the ad platform, that disagreement is the finding: take it to whoever owns the definition.
The same check, per channel: the channel figures should add up to the total, and spend per channel should match the ad platform’s own report for the same dates. In our example the nine revenue rows add to 849,177.41, the same as the total. We did not test the per-channel version against ad platform reports; it is our recommendation, not a result.
Two other things we tried are worth knowing:
- Counting rows in the result did not help. Four of the five wrong answers had the right number of rows, one per source. The fifth was a single total row with no grouping. Counting the rows left after each join did catch all five, as a zero-row signal, but that means running extra queries.
- Asking the assistant to check itself helped only a little. In a separate set of 12 Claude runs, we added “also give me a second query that checks your figure against an independent total”. The main queries were no better: strictly 8 of 12 were right (a ninth returned three per-status rows and no total), one failed with an error, and the two wrong return-on-ad-spend answers happened again. Of the 12 check queries, 8 ran and 4 were rejected by BigQuery. Seven of the 8 agreed with correct answers, with no false alarm. One caught one of the two silent failures, and only by accident: it reused the same status filter as the main query, so it could not say why the figure was wrong. That is one case, not a rule, but it is why we do not lean on a check the assistant writes. We did not run this arm on ChatGPT.
So the trust rule is short: a check you can rely on comes from a number that does not come from the assistant.
Where it goes quietly wrong
The results of the 24 plain runs, by model, counts only (two runs per question and model, so these are not rates):
| Claude (12 runs) | ChatGPT, free plan (12 runs) | |
|---|---|---|
| Passed BigQuery’s dry run | 12 | 12 |
| Ran | 12 | 12 |
| Matched our reference answer | 10 | 9 (6 with the exact unit) |
| Error message shown | 0 | 0 |
| Wrong with no error (empty, zero or NULL) | 2 | 3 |
| Stated its assumptions | 12 of 12 | 9 of 12 |
Three of ChatGPT’s nine matches gave a percentage (2.49 instead of 0.0249, or 67.5 instead of 0.675): two click-through rate answers and one share of order lines. We counted them as correct under a rule we fixed after the first ChatGPT reply came in, so strictly it is 6 of 12.
Four of the five wrong answers were the return-on-ad-spend question, for the reason above. The fifth was a ChatGPT run on the discount question that filtered on a status, 'delivered', that does not exist in the data. The total was empty, and the reply did not say it was a guess.
The mistakes we expected did not happen. Averaging per-row ratios and multiplying ad spend by joining it to sessions did not appear in any of the 24 runs. Both models added up first and divided afterwards.
For what invented SQL looks like, see AI-written SQL and hallucination. For how to read an accuracy figure someone quotes to you, see text-to-SQL accuracy benchmarks.
Name your dialect
People search for “ChatGPT for SQL Server” as if SQL were one thing. SQL Server’s version and BigQuery’s differ in basic words, so tell the assistant which you use. We did not test SQL Server. We ran two lines of the kind SQL Server accepts against BigQuery as dry runs on 14 October 2026, and both failed at once with a message: SELECT TOP 5 ... gave “Syntax error: Expected end of input but got integer literal “5”“, and DATEADD(day, -30, GETDATE()) gave “Function not found: DATEADD; Did you mean date_add?”. That is the safe kind of error, the opposite of the status spelling: you see it and paste it back. Google’s interactive SQL translator converts “a query from a different SQL dialect into a GoogleSQL query”; check its supported dialects for yours.
When the question is a business figure, not syntax
When the question is a business figure, not syntax
The status mistake is not really about SQL: the assistant guessed what “completed” means in your data. Revenue has the same problem, before or after refunds, with or without cancelled orders. See can you trust AI with your business numbers.
One way to take the guess out is to have an analyst write the definition once, as a Data Mart in OWOX Data Marts, and let the assistant work from published Data Marts through the OWOX MCP server. It “cannot run arbitrary SQL” (OWOX documentation, read October 2026). That lowers the risk of a wrong figure, but it is not a guarantee. In our own benchmark on published Data Marts, 20 of 22 questions were answered correctly, and one answer led with gross revenue where the reference was net. That is our demo data, not yours.
If you have no warehouse
The six steps and the total check apply to anyone. Paste the schema of whatever you have, and compare the total with a trusted figure. In one small test of ours, Claude was given a week’s order export (275 lines) through a coding tool’s file reader, not pasted into a chat. It gave correct figures in nine of nine runs and led with net revenue each time (the exported-file test). That was one model and one clean file, and we have no result for pasting a CSV into ChatGPT. The Data Mart and Sheets route below needs Google BigQuery or AWS Athena today, as the OWOX documentation lists. If you are unsure where your data lives, ask whoever built your reports: “Do our numbers come from a data warehouse, a spreadsheet export or a platform’s own report?”
Send the checked SQL to Google Sheets
Send the checked SQL to Google Sheets
If your data is in BigQuery or Athena, you can keep a checked query and have the figures arrive in a spreadsheet. The query runs in your warehouse and OWOX Data Marts publishes the result to Sheets. The documentation page for a SQL-based Data Mart walks through it: paste the checked SQL into a Data Mart, publish it, add a report that sends it to a Google Sheets destination, and optionally add a trigger (daily, weekly, monthly or at an interval). The Google Sheets destination page covers setting up the destination itself. The total check is still yours to do once. For more BigQuery SQL, see the BigQuery SQL guide.
How we tested
We wrote six business questions for a demo shop in Google BigQuery (the owox-demo.ecommerce dataset, Q3 2026): click-through rate, cost per click and ad cost per session by source or campaign, return on ad spend by source, discounts given away, and the share of order lines sold below list price. Before any run, we computed the correct answer for each from the data.
Each prompt was the pasted schema as text, the question as a business person would ask it, and “Write me the SQL.” Nothing else, and no hint about traps. We ran each query read-only, dry run first, and compared it with the reference. Two runs per question and model, 24 in all, on 14 October 2026.
- ChatGPT: the free plan, each run in a new temporary chat. The interface did not display a model name. The personalization setting read “Personalized” and could not be switched, so memory from earlier chats may have been available.
- Claude: fresh sub-agents given only the schema text. Six of the 12 replies came back without being saved, so we saved them verbatim from the sub-agent returns, and one reply in the check-request set was recovered from the workflow journal. The texts are unchanged. A further 12 runs added the check request described above; not run on ChatGPT.
Limits: one warehouse and one demo shop. We wrote the questions and the references. Neither model saw the data, so neither could look up how a status is spelled. This tells you what someone who pastes a schema gets. It does not tell you how either model behaves in general, how paid plans behave, or how a larger warehouse with untidier names would go.
ChatGPT writes SQL that runs, and you can't tell whether the number it returns is right before it goes to your boss.
Paste this into your AI assistant. It reads your actual schema, writes the report query, states its assumptions and checks the total against a number you already trust.
The prompt
Read https://www.owox.com/ai-agent-setup.md and follow it to set up OWOX Data Marts for me, starting from /blog/articles/how-to-build-sql-queries-with-chatgpt.md. The task: write me the SQL for return on ad spend by source against my own schema, list every assumption you made about values, and check the total against my ad platform's spend. Tag the links you give me with utm_campaign=ai-task and utm_term=b1. Use what you know about my context and goals
Prefer to start on your own? Get started free
Frequently Asked Questions
Can ChatGPT automatically correct errors in SQL queries?
Only the errors you can see. ChatGPT does not run your query or see your data unless you connect it, so it can correct only what you paste back to it. Loud errors are the easy case: in our check, two SQL Server-style lines run against BigQuery failed at once with a clear message, which you can paste back (we did not test how well either assistant then fixes it). Quiet errors are the hard case: 5 of our 24 test queries returned an empty or zero result with no error at all, so there was nothing to paste back. In a separate set of 12 Claude runs, asking the assistant to write its own check helped only a little; we did not run that on ChatGPT. The reliable check is a total you compare with a number you already trust. See Using ChatGPT to write SQL for your reports: six steps and a check.
How does ChatGPT handle SQL query verification?
It does not verify a query against your data. In our test of the ChatGPT free plan, 9 of 12 replies listed the assumptions behind the query, and Claude listed them in 12 of 12; reading that list is the closest thing to verification the assistant gives you. A BigQuery dry run only shows that a query is valid: all 24 of our test queries passed it, including the five that returned wrong results. What caught those five was comparing the total with a figure we already knew. Results are for ChatGPT's free plan and Claude on six questions on one demo warehouse, 14 October 2026. See Using ChatGPT to write SQL for your reports: six steps and a check.
Can ChatGPT generate complex SQL queries?
It can write queries that run, but "runs" is not "right". In our test of the ChatGPT free plan, all 12 queries ran and 9 matched our reference answer (6 if the unit has to be exact, because three answers came back as a percentage). Claude's 12 queries also all ran, and 10 matched. The questions were six business figures such as return on ad spend and share of order lines sold below list price, two runs each on one demo warehouse. That does not tell you how either assistant behaves on other data, other plans or other models. Check the total before you use the figure. See Using ChatGPT to write SQL for your reports: six steps and a check.
Does ChatGPT support visualization of database schemas?
Our test did not cover schema diagrams, so we make no claim about it. What the method in the article uses is the schema as text: a list of your tables, their columns and the type of each, pasted into the chat. The assistant cannot see your data, so it only knows the tables and columns you tell it about. If you do not have the schema, ask whoever owns the data. For any column you will filter on, add its allowed values too. See Using ChatGPT to write SQL for your reports: six steps and a check.
How secure is using ChatGPT for SQL query generation?
The method in our article sends only text: your table and column names and your question. It needs no connection to your data, and in our test neither ChatGPT nor Claude saw the data itself. Your table and column names still leave your company when you paste them, so decide whether that is acceptable for your schema. We did not assess the security or data-handling terms of ChatGPT or any other assistant; check your provider's terms and your company's policy before you paste anything. See Using ChatGPT to write SQL for your reports: six steps and a check.
Can ChatGPT integrate with Google BigQuery?
We did not test an integration. In our test, ChatGPT (free plan) and Claude only received text, the schema of the tables and the question, and returned SQL. We ran each query in BigQuery ourselves, as a dry run first and read-only. Tell the assistant the dialect in the first line ("Google BigQuery (GoogleSQL)"), because each database speaks its own version of SQL. Whether a connector or another way of linking an assistant to BigQuery works for you is outside what we tested. See Using ChatGPT to write SQL for your reports: six steps and a check.
How does ChatGPT optimize SQL queries?
Our test did not measure optimization, so we make no claim about speed or cost. We tested whether the answer is right: six business questions on a demo warehouse, on the ChatGPT free plan and on Claude. If an assistant rewrites a query to make it faster, treat the new query as a new query: run it and compare its total with the number you trust before you rely on it. See Using ChatGPT to write SQL for your reports: six steps and a check.
Is ChatGPT capable of debugging SQL queries?
We did not test debugging. What we saw is the two kinds of failure. A loud error, such as SQL Server syntax run in BigQuery, shows a message you can paste back to the assistant with the query; how well it then fixes the query we did not test. A quiet error shows no message: in our test both ChatGPT (free plan) and Claude wrote `status = 'completed'` where the data has `Completed`, the filter matched nothing and the figure came back empty or zero. There is nothing to debug until you notice the total is wrong. See Using ChatGPT to write SQL for your reports: six steps and a check.
Why is the SQL ChatGPT wrote giving wrong numbers?
In our test the usual cause was a guess about a value, not a syntax mistake. The schema listed the column `status` but not its values, and both ChatGPT (free plan) and Claude wrote `status = 'completed'` where the data has `Completed`. The filter matched no orders, so the query ran without an error and returned empty (Claude) or 0.0 (ChatGPT). Four of the five wrong answers in our 24 runs were that question, return on ad spend; the fifth was a ChatGPT run that filtered on a status, `'delivered'`, that does not exist in the data. The other cause to look for is a definition: the assistant answers the question as you asked it, which may not be the figure you meant. To reduce the guessing, add the allowed values of any column you filter on (we did not test whether that fixes it), read the assumptions listed under the query, and compare the total with a number you trust. See Using ChatGPT to write SQL for your reports: six steps and a check.
How do I check SQL written by ChatGPT without reading it?
Compare the total with a number that does not come from the assistant: your shop's sales report, the finance figure for the same period, the ad platform's report for spend. The two must use the same definition and the same period, and any difference must be one you can explain, such as refunds, shipping or timing. In our test this caught all five wrong answers, which were the loud kind (empty, zero or NULL). It does not prove a figure is right: a total can match while the split between channels is wrong. A query that runs, or passes a BigQuery dry run, has told you only that it is valid. Do not lean on a check the assistant writes for itself: in 12 Claude runs, the self-written checks helped only a little. See Using ChatGPT to write SQL for your reports: six steps and a check and, for reading an accuracy figure someone quotes to you, text-to-SQL accuracy benchmarks.
