Skip to main content
Back to SQL Coding

SQL Coding · Doing It With AI

Spot the Bug AI SQL

Spot the Bug: AI-Generated SQL

The one idea: An AI can write a query in seconds that looks perfect and still be quietly wrong in a way that changes the business answer. Your job isn't to write the fix — it's to run a short checklist that exposes the bug, then hand it back.
⚠️ The trap: The AI attached a fluent explanation of what its query does, so that's what the query actually does. That feels right and it's wrong. The explanation is a claim about the code, not proof of it — the SQL routinely computes something else (an INNER JOIN where the words say "all customers"); check the FROM/JOIN/GROUP BY against the sentence.

An AI can write a SQL query in two seconds that looks completely correct — right tables, right columns, plausible logic — and still be quietly wrong in a way that changes the business conclusion. Your job isn't to write the fix. It's to know when to ask for one.

Why AI-generated SQL fails in specific, predictable ways

AI models are excellent at producing syntactically correct SQL and good at common patterns, but they don't run the query against your actual data before showing it to you. That means every trap from earlier sections — join row-count changes, NULL handling, GROUP BY grain — shows up in AI output at the same rate it shows up in human-written SQL, except it arrives with more confident phrasing ("Here's a query that shows total active users...").

Worked example 1 — the confident wrong join

Ask: "How many total customers do we have, broken down by whether they've made a purchase?"

**AI-generated…

Keep reading: Spot the Bug AI SQL

Sign up free to read the full lesson, ask the AI tutor, and take practice questions.

  • Full lesson content
  • AI tutor for this section
  • Practice questions