Four AI-SQL Traps That Fool Competent Analysts
The one idea: The dangerous AI-written queries aren't the ones that error out — they run fine, return a clean number, and are still wrong. These four bugs survive review because the query looks reasonable and the answer looks plausible. Learn the shape of each one so you can catch it before it ships in a board deck.
⚠️ The trap: The breakdown adds up to the correct grand total, so the query underneath it must be right. That feels right and it's wrong. Three of these four bugs leave the grand total correct while quietly corrupting the middle rows — check an intermediate row and a zero case, never just the sum.
The earlier "spot the bug" lesson covered the mistakes a careful reader catches on the first pass — wrong join type, a NULL eaten by a SUM. These four are harder. Each one produces a query that runs without complaint, returns a believable result, and passes a casual read. They fool people who genuinely know SQL. AI writes all four constantly, because each depends on a default the model quietly accepts.
The running total that stalls on a tie
A running total is built with a window function — a calculation that runs across rows without collapsing them, written with OVER (...). When you write OVER (ORDER BY day), SQL has to decide which rows count toward "so far." It has two ways to do that, and the one it picks by default is almost never the one you want.
ROWS("count physical rows up to and including this one") gives a true row-by-row running…
Keep reading: Hard AI SQL Traps
Unlock the full SQL Coding track — every lesson, the AI tutor, and full mock assessments.
- Full lesson content
- AI tutor for this section
- Practice questions