DEV Community

Cover image for Common Mistakes AI Makes When Generating SQL
Gia
Gia

Posted on

Common Mistakes AI Makes When Generating SQL

AI has made writing SQL much faster.

Today you can type something like “show me the top five customers by revenue this year,” and an AI assistant will generate a query in seconds. It’s impressive — but it’s not always correct.

After experimenting with several AI-powered SQL tools, I noticed a pattern. Most mistakes aren’t because the AI doesn’t understand SQL. They happen because it doesn’t fully understand your database, your business rules, or what you’re actually trying to ask. If you’re relying on AI to generate SQL, here are the mistakes to watch for.

The seven failure modes — almost none are about SQL syntax.

Seven common AI SQL mistakes at a glance

The seven failure modes — almost none are about SQL syntax.

1. Guessing table or column names

One of the most common issues is that the AI assumes your schema looks a certain way. Ask it to “show all active customers” and it might generate:

SELECT *
FROM customers
WHERE status = 'Active';
Enter fullscreen mode Exit fullscreen mode

Looks perfectly reasonable — until you realize your database has a table called users, and the column is named is_active. The SQL is valid; it just doesn’t match your schema.

Valid, runnable, and wrong — the query assumes a schema you don’t have.

Valid SQL that points at the wrong table and column

Valid, runnable, and wrong — the query assumes a schema you don’t have.

How to avoid it

Give the AI access to your schema, or name the correct tables in your prompt. The more context it has, the fewer assumptions it has to make.

2. Missing JOIN conditions

AI is good at spotting relationships, but it sometimes  JOINS the wrong tables — or forgets to join them at all. Ask it to “show every order along with the customer’s name,” and a bad join condition can produce duplicate rows or completely inaccurate results. These are easy to miss because the query still runs successfully.

How to avoid it

Always review joins before running a query, especially when multiple tables are involved.

3. Misunderstanding business terms

Business language isn’t always obvious. For one company, “active customers” means users who logged in within the last 30 days; for another, it means customers with an active subscription. The AI doesn’t know your definitions — it picks the most likely interpretation, which may not be yours.

How to avoid it

Be specific. Instead of “show active customers,” try “show customers who placed at least one order in the last 30 days.” The clearer the prompt, the better the query.

4. Forgetting important filters

Sometimes the AI generates a query that technically answers your question but includes more data than intended. Ask for “this month’s sales” and the SQL might not filter by the current month at all, or it might use the wrong date column. Small omissions like these can completely change the results.

How to avoid it

Double-check every filter — especially dates, regions, and status conditions.

5. Incorrect aggregations

Aggregations are surprisingly tricky. Ask “what’s the average order value by customer?” and the AI might average every individual order — when you wanted the average revenue per customer. Both queries are valid, both produce numbers, but only one answers your actual question.

How to avoid it

Look carefully at GROUP BYCOUNT()SUM(), and AVG() before trusting the output.

6. Ignoring NULL values

NULL values are a classic source of bugs, even for experienced developers, and the AI sometimes forgets to account for them. That can lead to incorrect counts, unexpected averages, or missing rows.

How to avoid it

Check whether the query should use conditions like IS NULLIS NOT NULL, or COALESCE(). These small details often make a big difference.

7. Optimizing for correctness, not performance

Most AI tools focus on generating SQL that works — not SQL that performs well. A query might scan millions of rows unnecessarily, use inefficient joins, skip indexes, or return more columns than needed. On a small database you won’t notice. On a production system, it becomes a real problem.

How to avoid it

Review  execution plans, use indexes wisely, and avoid selecting data you don’t actually need.

AI is a great assistant — not an autopilot

AI is excellent at speeding up repetitive work. It can help you write queries faster, explain unfamiliar SQL, and even suggest improvements. But it’s still your job to verify the output. Treat AI-generated SQL the way you’d review code from a teammate: read it, understand it, test it, then run it.

Review AI SQL the way you’d review a teammate’s pull request.

Read, understand, test, run

Review AI SQL the way you’d review a teammate’s pull request.

Every one of these mistakes is invisible until someone reads the query.

Notice what almost none of these failures are about: SQL syntax. The AI can write a flawless query against a database it doesn’t quite understand — valid, fast to produce, and pointed at the wrong thing. That’s the whole story of working with these tools. The generation was never the hard part; closing the gap between a plausible-looking answer and one you can actually trust is. Give the model your context, keep a human in the loop, and that gap gets small enough to cross with confidence. That’s exactly the bet behind tools like DBx — lean on your schema, and keep the generated SQL in plain sight so you’re reviewing an answer, never trusting a black box.

Top comments (0)