AI assistants write SQL quickly, and most of it runs. Running is not the same as being right. This is the short routine we teach for checking AI-written SQL before a number goes into a report.
An example that runs and is wrong
Suppose you ask an assistant for "revenue per customer from completed orders" and it returns this query. Tables: customers, orders (one row per order, with amount) and order_items (one row per product line).
SELECT c.name, SUM(o.amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items i ON i.order_id = o.order_id
WHERE o.status = 'completed'
GROUP BY c.customer_id, c.name;
| name | revenue |
|---|---|
| Asha | 690.00 |
| Ben | 400.00 |
| Cara | 120.00 |
| Eli | 60.00 |
It runs, the names look right and the numbers look plausible. But order_items has more than one line for some orders, so the join repeats those orders and SUM(o.amount) counts them more than once. Asha has an order of 250 with two item lines, so 250 is added twice.
The check that catches it: reconcile to a known total
SELECT SUM(amount) FROM orders WHERE status = 'completed';
Completed orders add up to 1,030. The query's customers add up to 1,270. The numbers disagree, so something is double-counted. Removing the unnecessary join gives:
SELECT c.name, SUM(o.amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'completed'
GROUP BY c.customer_id, c.name;
| name | revenue |
|---|---|
| Asha | 440.00 |
| Ben | 400.00 |
| Cara | 120.00 |
| Eli | 60.00 |
These add up to 1,020. The remaining 10 is a completed order with no customer ID. That is now a known, explainable difference, which you can mention in a footnote, instead of a hidden error.
A five-step routine for any AI-written query
- Read it before you run it. Say in plain words what each join and filter does. If you cannot, do not use the result yet.
- Count rows. Check the row count before and after each join. A count that grows unexpectedly is a fan-out.
- Reconcile one total. Compare the query's grand total with a simple, trusted query or with the finance report.
- Test an edge case. Pick one customer or one day and work out the answer by hand.
- Log what you checked. One line per check in your notes or README. It makes your work reviewable, and it is good portfolio evidence.
Prompts that help
Give the assistant the table structures and the grain of each table ("orders has one row per order; order_items has one row per product line"). Ask it to explain each join and to list the checks it would run. Then run those checks yourself. The assistant can suggest; you are responsible for the number.
This is what we mean by using AI like a professional, not as a shortcut: faster drafts, the same standard of proof.


