Let AI Write Your SQL, But Read It Before You Run It

Typing "show me customers who haven't ordered in 90 days" and getting back a working SQL query feels like magic the first few times. It is also exactly the kind of convenience that gets people into trouble, because a SQL query that runs without errors and a SQL query that does what you actually meant are two very different things.

The risk isn't that AI-generated SQL is usually wrong. It's that when it's wrong, it's wrong in a way that looks completely plausible and executes without complaint.

Where plain-English requests break down

SQL has to be exact about things human language is casual about. "Customers who haven't ordered in 90 days" is ambiguous in ways you might not notice until you've already run the query: does it mean no orders at all in the last 90 days, or customers whose most recent order was more than 90 days ago (which excludes brand-new customers who've simply never ordered)? Does a cancelled or refunded order still count as an order? If a customer has no rows in the orders table at all, should they be included or excluded?

An AI generator will pick one interpretation and write confident, well-formatted SQL for it. It won't flag the ambiguity unless you ask. That's not a flaw unique to AI — a junior analyst would make the same silent assumption — but it means the query's confidence level and its correctness level are two separate things you have to check separately.

The three lines that matter most

When you get a generated query back, there are three parts worth reading closely before anything else:

  • The WHERE clause. This is where ambiguity hides. Check every condition against what you actually meant, especially date comparisons (> vs >=), NULL handling, and whether OR/AND groupings are parenthesized the way you'd expect.
  • JOIN type. An INNER JOIN silently drops rows that don't match, while a LEFT JOIN keeps them. If a generated query switches a LEFT JOIN to an INNER JOIN compared to what you need, your result set shrinks and nothing tells you rows are missing.
  • Anything that isn't a SELECT. UPDATE, DELETE, and DROP statements deserve a different level of scrutiny than a read-only query, because a mistaken WHERE clause on a DELETE doesn't give you a wrong answer — it gives you missing data. Always run the equivalent SELECT first to see exactly which rows match before converting it to a write operation.

Formatting isn't optional, it's how you catch mistakes

A generated query that comes back as one dense line is genuinely harder to review than it needs to be — nested subqueries and multiple JOIN conditions blur together when they're not indented. Before you review logic, get the query into a readable shape: consistent indentation, one clause per line, keywords aligned. It takes a few seconds and it's the difference between spotting a misplaced parenthesis and missing it.

This also matters for queries you didn't write yourself — pasted from a coworker, copied out of an old script, or pulled from documentation. A wall of unformatted SQL hides exactly the details you need to see clearly.

Treat it like a draft, not an answer

The honest way to use AI for SQL is the same way you'd use a capable but new team member: let it do the first draft, because turning a sentence into working syntax is genuinely tedious and AI is fast at it. But the review step — does this WHERE clause mean what I meant, does this JOIN keep the rows I need, is this safe to run against real data — is still yours. Skipping that step is how a harmless-looking report query turns into a production incident.

If you're regularly going from a plain-English request to a query, the AI SQL Generator handles the first draft. Once you have a query — generated, copied, or inherited from someone else — run it through the SQL Formatter before you read it closely. Formatted first, reviewed second, run last.