How to Use ChatGPT for SQL Without Shipping Wrong Numbers
A practical workflow for analysts: what to give the AI, what never to paste, and the three checks that catch confident-but-wrong queries before your stakeholders do.
ChatGPT, Claude and Copilot are now very good at writing SQL. That is exactly what makes them risky: a query that runs, looks reasonable, and returns a plausible number can still be wrong. The analysts getting real value from AI aren’t the ones who trust it most. They’re the ones with a fast routine for checking it.
Give it the schema, never the data
The AI doesn’t need your rows to write a query — it needs your tables, columns, and business rules. Sharing only the schema keeps you inside most company data policies and gets better results, because you spell out the things the AI would otherwise guess.
A prompt that works
You're a senior analyst. Postgres. Tables (schema only): orders(order_id, customer_id, order_date, amount, status) customers(customer_id, region, segment) Business rules: - Only status = 'completed' counts as revenue. - Each customer has exactly one row in customers. Return revenue by region for last quarter, highest first. Explain each step in one line.
Notice the business rules. Without them the AI will pick a status filter (or none) on its own, and you won’t notice unless you read the query line by line.
The three checks that catch most AI mistakes
- Row counts: does a join change the number of rows when it shouldn’t? This catches duplicate keys and fan-out — the most common silent error.
- A known answer: run the query for a period where you already know the result from a trusted dashboard or report.
- Edge cases: what happens with nulls, zero values, and the first or last period? AI often handles the happy path and ignores the edges.
Row-count check for a join
-- If the second number is bigger, customers has duplicate customer_ids -- and every revenue figure joined through it is inflated. SELECT COUNT(*) FROM orders WHERE status = 'completed'; SELECT COUNT(*) FROM orders o JOIN customers c ON c.customer_id = o.customer_id WHERE o.status = 'completed';
Ask it to explain, not just to write
The most underrated use of AI for SQL isn’t writing new queries — it’s explaining old ones. Paste an inherited query and ask what it does and what could make the results wrong. It is very good at spotting things like a LEFT JOIN that quietly behaves like an INNER JOIN because of a WHERE filter.
Use AI for the first draft and for a second opinion. Keep the final judgment — and the checks — for yourself.
What never to paste into a personal AI account
- Customer or employee personal data (names, emails, IDs that identify people).
- Confidential financials that haven’t been published.
- Credentials, connection strings, or API keys inside scripts.
- Anything your company’s AI policy rules out — if your employer provides an approved tool, use that one.
Start with one query this week
Pick a query you write every week, give the AI your schema and rules, and run the three checks on what comes back. Do it twice and the routine sticks. If you want help building this into your actual work, Coachly’s free AI Career Plan call is a good place to start — and our free guide has 15 worked examples like this one.
Want a plan for your own job?
On a free 30-minute AI Career Plan call, we’ll find the parts of your work AI can take over and map your next 4 weeks.