Atterna
RUEN
Get access

AI-generated SQL — 6 mistakes that run without an error (and how to catch them)

On this page · 8

The dangerous AI query is not the one that fails — it is the one that runs and answers a slightly different question. Six of them, each one to solve right here:

1. The join that inflates a total

Asked for revenue, an assistant joins orders to their items and sums the order amount. Every order is repeated once per item.

Practice · check 1 of 6

orders.idamountitems in the order
1€503
2€301
SELECT SUM(o.amount)
FROM orders o
JOIN items i ON i.order_id = o.id;

What does this query return?

Preparing the practice…

On Atterna's practice marketplace this exact query turns €552,645 of paid orders into €1,239,938. Nothing errors; the number is just more than twice too big. Ask back: "Is any amount summed after a join to a table with several rows per order?"

2. Counting rows when you asked for people

Marketing asks "How many customers ordered?", and the assistant writes this:

Practice · check 2 of 6

SELECT COUNT(buyer_id)
FROM orders
WHERE status = 'paid';

Marketing asked for the number of customers. What does this query actually count?

Preparing the practice…

Ask back: "Does this count customers or orders? Use COUNT(DISTINCT …) for people."

3. A LEFT JOIN undone by WHERE

Assistants often add a filter on the joined table in WHERE. The rows the LEFT JOIN was meant to keep disappear.

Practice · check 3 of 6

SELECT u.id, COUNT(o.id) AS paid_orders
FROM users u
LEFT JOIN orders o ON o.buyer_id = u.id
WHERE o.status = 'paid'
GROUP BY u.id;

Do users with no paid orders appear in the result with 0?

Preparing the practice…

Ask back: "Should this condition be in ON, so rows without a match stay with zero?"

4. NULL quietly changes the average

Empty values don't raise errors. They change what an aggregate covers.

Practice · check 4 of 6

productrating
Wireless headphones4.6
Set of wine glasses4.5
Scented candleNULL
Travel mug4.8
SELECT AVG(rating) FROM catalog;

What does it return?

Preparing the practice…

Ask back: "How many rows have NULL in this column, and should they count as zero, be excluded or be reported separately?"

5. Grouping by a timestamp

You ask for orders per day, and the assistant groups by the column that holds the order time:

Practice · check 5 of 6

SELECT created_at, COUNT(*)
FROM orders
GROUP BY created_at;

You wanted orders per day. What do you get?

Preparing the practice…

6. A correct query that answers the wrong question

The last mistake isn't in the SQL at all. The query is right; the conclusion it is used for is not.

Practice · check 6 of 6

Tomás · marketing

Orders with AUTUMN20 were 2.5× bigger than orders without it! Can I put “AUTUMN20 makes baskets bigger” into the December plan?

-- the query is correct
AUTUMN20 orders   €91.81 average
without the code  €36.60 average

The query is correct and the numbers are real. What do you check first?

Preparing the practice…

This is where AI help stops being enough: the assistant can compute both averages, but it may not ask who could use the code in the first place unless you do.

A two-minute routine before you use AI-written SQL

  1. Say what one row of each table is, and what one row of the result should be.
  2. Look at every join: can it repeat a row you then sum or count?
  3. Look at every filter: is a condition on a LEFT JOINed table in WHERE instead of ON?
  4. Check the counts: does COUNT count the thing you were asked about, and does DISTINCT belong there?
  5. Check NULLs and dates: what happens to missing values, and is the latest period complete?
  6. Read the conclusion separately from the query: does the number support the claim, or only sit next to it?

Paste the answer or the SQL into the AI answer checker and it flags joins that repeat rows, LEFT JOINs undone by WHERE, = NULL and incomplete periods, then writes the follow-up question for you. To build the habit on real mess — cancelled orders, refunds, baskets from several sellers — Start teaches SQL from zero (days 1–2 with no account, all ten free with an account) and case 1 puts a convincing, wrong number in front of you.

FAQ

Can I trust SQL written by ChatGPT or another AI assistant?

Trust it the way you would trust a colleague's first draft: it usually runs and is often right, but it can double-count after joins, drop rows in a LEFT JOIN or count the wrong thing. Check the joins, filters, counts and NULL handling before the number is used.

What is the most common mistake in AI-generated SQL?

There is no reliable ranking, but a frequent and costly one is summing or counting after a join to a table with several rows per record, which inflates totals without any error. The fix is to aggregate before joining or to join only what you need.

How do I check an AI's SQL if I'm still learning SQL?

Ask the assistant to state what one row of the result means, to show the row count before and after each join, and to explain every filter. Then compare a total with a simpler query on one table. Our checker flags common patterns, such as joins that repeat rows, in your browser; it can't run the query on your data.

Will AI replace the need to learn SQL?

AI makes writing SQL faster, which makes reading it more important. The work that remains is deciding what to count, catching a query that answers the wrong question and explaining what the number can't tell you.

0 of 6 checks answered

Try the remaining checks above.

Every answer opens an explanation, including a wrong one.

Free account

Practise this, free

A free account opens hands-on lessons on a realistic marketplace.

  • All 10 days of the SQL courseFrom your first table to JOINs and window functions
  • Meridian episode 1A full analyst week: the record that wasn’t
  • Your first portfolio projectFinished work as a ready GitHub project, with your SQL and charts
  • Progress on any deviceYour cheat sheet and review cards go with you
Create a free account Free · 30 seconds · no cardAlready have one? Sign in

Examples use Meridian, Atterna’s fictional practice marketplace.