Use AI to Write SQL Without Breaking Prod

I asked AI for a simple report query last year. Total revenue by customer, last 30 days. It came back in four seconds, clean formatting, nice aliases, even a comment at the top. I ran it, pasted the number into a deck, and moved on.

The number was wrong. Not a little wrong — almost double.

Nobody caught it for two days, because the query looked right. That's the whole problem with AI and SQL right now. It doesn't write broken SQL very often anymore. It writes confident SQL. The syntax runs, the result looks plausible, and the logic is quietly off in a way you only notice when finance asks why revenue jumped overnight.

I still use AI for SQL every day. I just don't trust it the same way. Here's the workflow I use now.

The bug isn't syntax. It's the grain.

First is the JOIN fan-out. You join orders to order_items to get customer totals, and one order with three items suddenly counts three times. AI loves to do this:

SELECT c.name, SUM(o.total_amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items i ON i.order_id = o.id
WHERE o.created_at >= CURRENT_DATE - INTERVAL 30 DAY
GROUP BY c.name;

That SUM counts the order total once per item. Four items, four times the revenue. The fix is boring: sum from the line items, or aggregate before you join. But you have to spot it first, and AI won't flag it.

Second is NULL. Ask for "all orders that aren't cancelled" and AI writes WHERE status != 'cancelled'. In SQL that also drops every row where status is NULL, because NULL isn't equal to anything. Those are usually the messy orders you wanted to see. You need WHERE status != 'cancelled' OR status IS NULL. One line, completely different answer.

Third is the wrong grain. You ask for "daily active users" and AI gives you daily logins. Or you ask for "churned customers" without defining churn — 30 days? 90? AI will pick one and not tell you it guessed.

AI is great at writing the SQL. You're still in charge of defining what the question means.

Give it the schema, not just the question

The biggest improvement I made was to stop pasting just the question. AI can't guess your schema, so it invents column names that sound right — user_id vs customer_id, created_at vs order_date.

Now I paste three things every time: the CREATE TABLE statements, two or three sample rows, and one sentence saying what a row is — "One row in orders = one checkout. One row in order_items = one product in that checkout."

Then I ask in plain English: "Give me a SELECT query only. Total revenue per customer for the last 30 days, one row per customer, revenue from order_items, not orders."

Naming the source of truth stops most fan-outs before they happen. I also ask it to list its assumptions at the end. If it says "I assumed one order = one item," I know to fix it.

A prompt I reuse looks like this:

Here is my schema. One row in orders is one checkout, one row in order_items is one product in that checkout. Write a SELECT-only query for total revenue per customer in the last 30 days. Sum quantity * unit_price from order_items. Don't use orders.total_amount. List any assumption you made.

It's just giving the model the context a junior dev would need, instead of expecting it to read your mind.

The 5-minute check before you trust the result

First, run it as SELECT only, with LIMIT 20. Eyeball the rows. Does the total for a customer you know look sane? Pick one and check their orders by hand. If you can't verify one row manually, you don't understand the query well enough to ship it.

Second, check the row count. Expected 400 customers and got 12,000 rows? Your grain is off. Expected 12,000 and got 400? You filtered too hard — probably a NULL, or an inner JOIN that dropped people.

Third, run EXPLAIN. You're looking for a full table scan where you expected an index, and a JOIN that blows up the row count before the GROUP BY. New Techie's MySQL EXPLAIN Plan Visualizer makes raw EXPLAIN a lot easier to read.

Fourth, test the edge cases. A customer with no orders, an order with no items, an order with a NULL status. AI queries almost always work on the happy path. They break on the weird rows.

The whole check takes five minutes. That bad revenue number took two days to untangle.

The UPDATE and DELETE rule

This part is non-negotiable. Never let AI write an UPDATE or DELETE and run it directly.

AI will happily write DELETE FROM users WHERE last_login < '2024-01-01'; and forget NULLs, test accounts, and users who never logged in but have active subscriptions.

My rule: write the SELECT first. Run it, look at the rows, count them. Only when that SELECT returns exactly the rows you mean to change do you turn it into an UPDATE or DELETE. Even then, do it in a transaction:

BEGIN;
UPDATE users SET status = 'inactive'
WHERE last_login < '2024-01-01' AND last_login IS NOT NULL;
-- check the row count, then COMMIT or ROLLBACK

And keep a backup first. CREATE TABLE users_backup_2026_11_30 AS SELECT * FROM users WHERE ... takes ten seconds. Restoring from a backup you don't have takes a lot longer.

Paste the query into the SQL UPDATE / DELETE Safety Checker on newtechie.com before you run it, too. It catches a missing WHERE, the NULL trap, and the "this will hit 2 million rows" surprise.

A workflow you can steal

Here's the whole thing, in order:

  1. Paste schema + sample rows + what one row means
  2. Ask for SELECT only, name the source of truth for any totals
  3. Run with LIMIT 20, spot-check one customer by hand
  4. Check row count, run EXPLAIN, test NULL / empty cases
  5. For changes: SELECT first, transaction, backup, then UPDATE / DELETE
  6. Save the final query with a one-line comment saying what it does and what it assumes

That's it. No fancy tooling, no new framework. Just a bit of friction in the right places.

AI has made writing SQL faster. It hasn't made understanding your data optional. The people who get burned aren't the ones who don't know SQL — they're the ones who stop reading the query because it looked clean.

Use AI like a fast junior who types well but doesn't know your business yet. Give it context, check its work on the edges, and never let it touch prod data without a SELECT first.

If you want a quick sanity check on a JOIN, try the SQL JOIN Fan-out Analyzer on newtechie.com. Paste the query, it shows you where the rows multiply. It's the check I wish I'd run on that revenue query last year.

Uploaded Image