{"id":"data2-sql-fanout-wrong-total-explain","audience":"analysts","category":"data","track":"data","technique":"verification","level":"advanced","title":"Explain why an AI-written SQL total is too high","free":false,"task":"An AI wrote a query that reports $482,000 of revenue, but finance says $310,000. Write exactly 3 labeled lines in plain text, under 100 words: Problem, Evidence and Fix. Evidence must use the exact form 'Order 1001: 3 rows x $200 = $600 instead of $200', and Fix must start with the word 'Sum' and say how to compute the total without repeating orders, with no code block.","materialLabel":"Query, result and sample data (attached to your prompt automatically)","material":"AI-written query (Postgres):\n\nSELECT SUM(o.total_usd) AS revenue\nFROM orders o\nJOIN order_items i ON i.order_id = o.order_id\nWHERE o.order_date >= '2025-10-01' AND o.order_date < '2025-11-01';\n\nResult: 482,000\nFinance's ledger for October: 310,000\n\nTables:\norders(order_id, order_date, total_usd)   -- one row per order, total_usd is the whole order total\norder_items(item_id, order_id, sku, qty)   -- one row per item on the order\n\nSample:\norders: order 1001, 2025-10-04, total_usd 200\norder_items: (1, 1001, A-10, 1), (2, 1001, B-20, 2), (3, 1001, C-30, 1)\norders: order 1002, 2025-10-05, total_usd 150\norder_items: (4, 1002, A-10, 3)\n\nNotes:\n- The analyst only wanted revenue, not item detail.\n- Orders have one to six items.\n- No orders were refunded in October.","rules":[{"type":"plainText"},{"type":"maxWords","max":100}],"mistakes":[{"title":"Blaming the date filter","why":"The AI suspects dates or timezones instead of the join.","fix":"Add: \"The join to order_items repeats each order once per item.\"","checks":["r0"]},{"title":"Abstract evidence","why":"The AI says 'rows are duplicated' without an example.","fix":"Add: \"Evidence: order 1001 has 3 items so $200 counts as $600.\"","checks":["r1"]},{"title":"Pasting a new query","why":"The AI rewrites a whole query in a code block.","fix":"Add: \"Fix in one sentence, no code block: sum from orders alone.\"","checks":["r2"]},{"title":"Leaving the format to chance","why":"The AI picks its own length and layout unless told the exact shape.","fix":"Add: \"Plain text, no bold or headings.\"","checks":["plainText"]},{"title":"Trusting the AI to count words","why":"AI is bad at counting. Ask for a total word limit and it often lands over it.","fix":"Cap each part instead, for example \"each line under 15 words.\"","checks":["maxWords"]}],"promptHints":[{"label":"Asks for three labels","patterns":["problem:","evidence:","fix:"]},{"label":"Names the join cause","patterns":["join","repeat","fan.out","once per item"]},{"label":"Uses the order example","patterns":["1001","\\$600","3 times"]},{"label":"Asks for a concrete fix","patterns":["sum from orders","pre.?aggregate"]},{"label":"Rules out code","patterns":["no code block"]}],"hint":"Debugging means pointing at the row-level cause with a number, then giving a fix that is small and specific.","mission":"Why is the revenue total wrong in this SQL?","musts":["Three labeled lines","Evidence from order 1001","Concrete fix, no code"],"rubricLabels":["Problem says the join to order_items repeats each order once per item (a fan-out), so order totals are added more than once","Evidence is in the form 'Order 1001: 3 rows x $200 = $600 instead of $200'","Fix starts with the word Sum and says to sum total_usd from orders without joining items (or pre-aggregate items to one row per order first), and has no code block"]}