<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prachit Parikh</title>
    <description>The latest articles on DEV Community by Prachit Parikh (@prachitparikh).</description>
    <link>https://dev.to/prachitparikh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4221398%2F699dc5b1-c8f9-47df-8e44-3b735cb1e4e7.jpg</url>
      <title>DEV Community: Prachit Parikh</title>
      <link>https://dev.to/prachitparikh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prachitparikh"/>
    <language>en</language>
    <item>
      <title>Text-to-SQL answer receipts: the spec I'd write before an AI agent answers one data question</title>
      <dc:creator>Prachit Parikh</dc:creator>
      <pubDate>Sun, 22 Nov 2026 21:22:16 +0000</pubDate>
      <link>https://dev.to/prachitparikh/text-to-sql-answer-receipts-the-spec-id-write-before-an-ai-agent-answers-one-data-question-1jm2</link>
      <guid>https://dev.to/prachitparikh/text-to-sql-answer-receipts-the-spec-id-write-before-an-ai-agent-answers-one-data-question-1jm2</guid>
      <description>&lt;p&gt;Picture a sales director typing into the new AI data assistant on a Monday morning: "What was revenue last month?" A number comes back in four seconds. $8,550. Clean, confident, no caveats. She pastes it into a deck for the 10 AM leadership call.&lt;/p&gt;

&lt;p&gt;Finance's number for the same month is $5,550.&lt;/p&gt;

&lt;p&gt;Nobody wrote bad SQL. The agent's query ran. It joined nothing it shouldn't have, and it didn't make up a table. It picked one reasonable reading of "revenue last month" out of at least five, and it didn't tell anyone it had picked. In the fixture below, those five readings range from $5,550 to $8,550, a 54% spread on a ten-row table.&lt;/p&gt;

&lt;p&gt;That's the failure I think BAs should be writing requirements for right now. A text-to-SQL agent rarely fails by writing broken SQL. It fails by writing correct SQL for a question nobody asked. So in this post I'll give you a spec for &lt;strong&gt;answer receipts&lt;/strong&gt;: what the agent has to return next to every number, when it has to stop and ask, and how you test it. There's a runnable SQLite fixture, a filled receipt, and the acceptance criteria I'd hand to the team building it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefer to watch?&lt;/strong&gt; The receipts idea started for me with AI-written user stories, where every story has to quote the meeting it came from. Same principle, different artifact:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/cXMIEbvokcc" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Video: &lt;a href="https://www.youtube.com/watch?v=cXMIEbvokcc" rel="noopener noreferrer"&gt;How to Write User Stories with AI (Without Made-Up Requirements)&lt;/a&gt;, my 6-minute walkthrough of the receipts method for stories.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Credit where it's due
&lt;/h2&gt;

&lt;p&gt;The phrase "answer receipt" isn't mine. It comes from a post by AI Explore on Dev.to last week, &lt;a href="https://dev.to/aiexplore369zoho/text-to-sql-agents-answer-questions-nobody-reviewed-add-receipts-123h"&gt;Text-to-SQL Agents Answer Questions Nobody Reviewed. Add Receipts&lt;/a&gt;. The core argument: a dashboard is a question somebody reviewed, while a text-to-SQL answer is a question nobody reviewed, so the agent should return "a structured record of the choices it made" instead of a bare number. The post lists what goes on it (the metric and its definition by identifier, grain, filters, time range, timezone, any defaults the user never specified, the SQL, row counts at key stages) and gives a rule for when to ask: generate several plausible interpretations, run them, and only ask the user when the results diverge enough that "the ambiguity is material."&lt;/p&gt;

&lt;p&gt;It also has the line I keep coming back to: an agent that's right 90% of the time and hides which 10% is worse than one that's right 80% of the time and tells you where to look.&lt;/p&gt;

&lt;p&gt;What I want to add is the BA layer. That post is written for the engineer building the agent. This one is for the person who has to write the acceptance criteria, pick the tolerance, and sign the thing off. Those are different jobs, and the second one is where most of these projects go quiet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why now
&lt;/h2&gt;

&lt;p&gt;Three things make this a this-quarter problem rather than a someday problem.&lt;/p&gt;

&lt;p&gt;First, the tools are shipping. Power BI's Copilot now supports &lt;a href="https://learn.microsoft.com/power-bi/create-reports/copilot-prepare-data-ai-verified-answers" rel="noopener noreferrer"&gt;verified answers&lt;/a&gt;: "human-approved, visual responses in Copilot that predefined phrases trigger." Microsoft's page (updated 10 September 2026) allows up to 250 per semantic model and 15 trigger phrases each. Snowflake's Cortex Analyst has a &lt;a href="https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst/verified-query-repository" rel="noopener noreferrer"&gt;Verified Query Repository&lt;/a&gt;: "a collection of questions and corresponding SQL queries to answer them," with optional &lt;code&gt;verified_by&lt;/code&gt; and &lt;code&gt;verified_at&lt;/code&gt; fields. Both vendors have arrived at the same idea. Reviewed answers get special treatment. Everything else is improvised.&lt;/p&gt;

&lt;p&gt;Second, the improvised part is still weak on real schemas. The &lt;a href="https://arxiv.org/abs/2409.02038" rel="noopener noreferrer"&gt;BEAVER benchmark&lt;/a&gt; from MIT and others is built from real private data warehouses: 9,128 question-SQL pairs over 812 tables, with an average of 101.5 tables and 869.4 columns per database. In its latest version (v3, 13 May 2026) the authors report that "state-of-the-art agentic frameworks using the advanced model GPT-5.2 achieve only 10.8% accuracy," rising to 30.1% when given oracle hints for every subtask. That's on enterprise schemas, not the tidy academic ones where the same models look great.&lt;/p&gt;

&lt;p&gt;Third, the people doing the work are already saying it. Hilal Ud Din's October post on IIBA's Analyst Catalyst blog, &lt;a href="https://www.iiba.org/business-analysis-blogs/where-ai-helps-my-business-analysis-work-and-where-i-draw-the-line/" rel="noopener noreferrer"&gt;Where AI Helps My Business Analysis Work (And Where I Draw the Line)&lt;/a&gt;, puts it in one sentence: "A confident wrong number is worse than no number."&lt;/p&gt;

&lt;p&gt;So the question isn't whether your company will get a text-to-SQL agent. It's whether anyone writes down what it has to show its work on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fixture: one question, five honest answers
&lt;/h2&gt;

&lt;p&gt;Here's the setup, on fictional Northstar Systems data. Ten orders, three refunds. The user is in New York and asks on 16 November 2026: "What was revenue last month?"&lt;/p&gt;

&lt;p&gt;Two edge rows matter. Order 1001 was placed at 02:30 UTC on 1 October, which is 10:30 PM on 30 September in New York. Order 1009 was placed at 01:40 UTC on 1 November, which is 9:40 PM on 31 October in New York. (US daylight saving time ends at 2 AM on 1 November 2026, so New York's October runs from 04:00 UTC on 1 October to 04:00 UTC on 1 November.) There's also a refund on 2 October for a September order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":memory:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executescript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""
CREATE TABLE orders (order_id INTEGER PRIMARY KEY, customer_id INTEGER,
                     created_at_utc TEXT, status TEXT, amount REAL);
CREATE TABLE refunds (refund_id INTEGER PRIMARY KEY, order_id INTEGER,
                      refunded_at_utc TEXT, amount REAL);
INSERT INTO orders VALUES
 (1001, 7, '2026-10-01 02:30:00', 'shipped',   1200.00),  -- 30 Sep 22:30 in New York
 (1002, 8, '2026-10-03 15:00:00', 'shipped',    850.00),
 (1003, 9, '2026-10-09 18:20:00', 'refunded',   400.00),
 (1004, 7, '2026-10-14 13:05:00', 'shipped',   2300.00),
 (1005, 10,'2026-10-18 20:45:00', 'cancelled',  600.00),
 (1006, 11,'2026-10-22 11:10:00', 'shipped',   1750.00),
 (1007, 12,'2026-10-27 16:00:00', 'pending',    950.00),
 (1008, 8, '2026-10-30 21:15:00', 'shipped',    500.00),
 (1009, 13,'2026-11-01 01:40:00', 'shipped',   1100.00),  -- 31 Oct 21:40 in New York
 (1010, 14,'2026-09-29 14:00:00', 'shipped',    700.00);
INSERT INTO refunds VALUES
 (1, 1003, '2026-10-12 10:00:00', 400.00),
 (2, 1006, '2026-10-25 09:30:00', 250.00),
 (3, 1010, '2026-10-02 12:00:00', 700.00);
"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;UTC_OCT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"'2026-10-01' AND created_at_utc &amp;lt; '2026-11-01'"&lt;/span&gt;
&lt;span class="no"&gt;NY_OCT&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"'2026-10-01 04:00:00' AND created_at_utc &amp;lt; '2026-11-01 04:00:00'"&lt;/span&gt;
&lt;span class="no"&gt;OK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"status IN ('shipped','refunded')"&lt;/span&gt;

&lt;span class="n"&gt;readings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="s2"&gt;"A gross, UTC month, all statuses"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"SELECT SUM(amount) FROM orders WHERE created_at_utc &amp;gt;= {UTC_OCT}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;"B gross, UTC month, shipped+refunded"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"SELECT SUM(amount) FROM orders WHERE {OK} AND created_at_utc &amp;gt;= {UTC_OCT}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;"C net of refunds in month, UTC month"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"""SELECT (SELECT SUM(amount) FROM orders WHERE {OK} AND created_at_utc &amp;gt;= {UTC_OCT})
      - (SELECT COALESCE(SUM(amount),0) FROM refunds
         WHERE refunded_at_utc &amp;gt;= '2026-10-01' AND refunded_at_utc &amp;lt; '2026-11-01')"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;"D net of refunds in month, New York month"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"""SELECT (SELECT SUM(amount) FROM orders WHERE {OK} AND created_at_utc &amp;gt;= {NY_OCT})
      - (SELECT COALESCE(SUM(amount),0) FROM refunds
         WHERE refunded_at_utc &amp;gt;= '2026-10-01 04:00:00' AND refunded_at_utc &amp;lt; '2026-11-01 04:00:00')"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="s2"&gt;"E net, refunds tied to the order's month, New York"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"""SELECT SUM(o.amount) - COALESCE(SUM(r.amount),0)
       FROM orders o LEFT JOIN refunds r ON r.order_id = o.order_id
       WHERE o.{OK} AND o.created_at_utc &amp;gt;= {NY_OCT.replace('created_at_utc','o.created_at_utc')}"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;readings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{k:52} {v:&amp;gt;10,.2f}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"spread vs lowest: {(hi - lo) / lo:.1%}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output when I ran it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A gross, UTC month, all statuses                       8,550.00
B gross, UTC month, shipped+refunded                   7,000.00
C net of refunds in month, UTC month                   5,650.00
D net of refunds in month, New York month              5,550.00
E net, refunds tied to the order's month, New York     6,250.00
spread vs lowest: 54.1%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every one of those queries is valid SQL. Every one answers a question a reasonable person might mean by "revenue last month." A grader that only checks "did the query run and return a number" would pass all five.&lt;/p&gt;

&lt;h3&gt;
  
  
  The row I didn't expect
&lt;/h3&gt;

&lt;p&gt;Look at C and D. They differ by only $100, and that's the dangerous part. The timezone choice moves two orders in opposite directions: order 1001 ($1,200) drops out of October in New York, and order 1009 ($1,100) drops in. The two errors nearly cancel. If you tested the agent by comparing its UTC answer to finance's New York answer on this one month, you'd see a 1.8% gap and call it rounding.&lt;/p&gt;

&lt;p&gt;Next month the edge orders won't line up so neatly. That's why a single matching total is weak evidence, and why I'd always put row counts on the receipt. D counts 6 eligible orders. C counts 6 too, but they're a different 6. Same count, same-ish total, different rows. Only an order-ID-level comparison catches it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which one is right?
&lt;/h3&gt;

&lt;p&gt;None of them, until somebody decides. That's the point. For Northstar, say finance books refunds in the month they happen and reports in New York time. That's reading D, $5,550. The agent's first instinct, A, is $3,000 too high because it counted a cancelled and a pending order and ignored refunds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7p1mq6o5eq0zf13l69r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7p1mq6o5eq0zf13l69r.png" alt="A star schema: one fact table in the middle with dimension tables around it" width="800" height="502"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A star schema answers "what's the grain?" by design: one fact table, one row per event. A text-to-SQL agent working on raw tables has to guess the grain, the statuses and the time zone every time. Image: &lt;a href="https://commons.wikimedia.org/wiki/File:Star-schema.png" rel="noopener noreferrer"&gt;"Star-schema.png"&lt;/a&gt; by SqlPac, &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/" rel="noopener noreferrer"&gt;CC BY-SA 3.0&lt;/a&gt;, via Wikimedia Commons.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The receipt spec
&lt;/h2&gt;

&lt;p&gt;Here's what I'd require next to every number the agent returns. I've written it as a JSON shape because that's what the engineers will build, but each field is really a business rule.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"answer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;5550.00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"unit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"USD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"question_as_asked"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"What was revenue last month?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"metric"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"net_revenue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"v2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"certified"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"definition"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Shipped or refunded order amounts minus refunds issued in the period"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"certified_by"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Finance reporting lead"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"certified_on"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-10-05"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"grain"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"one row = one order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"period"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"label"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"October 2026"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"timezone"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"America/New_York"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"utc_start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-10-01T04:00:00Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"utc_end_exclusive"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-01T04:00:00Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"how_chosen"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"default: user's profile timezone"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"filters"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"field"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders.status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"rule"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"IN ('shipped','refunded')"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"metric definition"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"defaults_applied"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"'last month' = previous calendar month in the user's timezone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"refunds counted in the month issued, not the order's month"&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"row_counts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"orders_in_period_any_status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"orders_after_status_filter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"refunds_in_period"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"interpretations_checked"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"interpretation_spread"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"54.1% (resolved by certified metric)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"sql"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"SELECT ... (full text)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"data_freshness"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders loaded 2026-11-16 06:10 ET"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"answer_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"certified"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four fields do most of the work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;metric.status&lt;/code&gt;.&lt;/strong&gt; Either &lt;code&gt;certified&lt;/code&gt; (a person signed off this definition, like a Power BI verified answer or a Snowflake verified query) or &lt;code&gt;improvised&lt;/code&gt; (the agent built it on the fly). The user interface must show the difference. AI Explore's post makes the same point: users need to tell certified answers from improvised ones. I'd go further and make it the first thing on screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;defaults_applied&lt;/code&gt;.&lt;/strong&gt; Every choice the user didn't make and the agent did. This is the field I'd fight hardest for, because it's the one that turns a silent guess into a visible one. If the list is empty on a question like "revenue last month," the agent is either lying or the metric definition is unusually complete.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;row_counts&lt;/code&gt;.&lt;/strong&gt; At least one count before filters and one after. A total with no count can't be compared to anything. Two counts let a finance analyst check the answer against their own system in a minute. (A reader called ontomato put it well in &lt;a href="https://dev.to/ontomato/comment/3khh3"&gt;a reply on Dev.to&lt;/a&gt;: two numbers end the argument faster than another meeting.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;interpretations_checked&lt;/code&gt; and &lt;code&gt;interpretation_spread&lt;/code&gt;.&lt;/strong&gt; Proof that the agent looked for other readings, and how far apart they landed. This is what drives the ask-or-answer decision below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F2%2F25%2FReceipt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F2%2F25%2FReceipt.jpg" alt="A printed shop receipt" width="800" height="1422"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A till receipt lists what you were charged for, line by line, so you can check it before you leave the shop. That's the bar for an AI data answer too. Image: &lt;a href="https://commons.wikimedia.org/wiki/File:Receipt.jpg" rel="noopener noreferrer"&gt;"Receipt.jpg"&lt;/a&gt; by Deavmi, &lt;a href="https://creativecommons.org/licenses/by-sa/3.0/" rel="noopener noreferrer"&gt;CC BY-SA 3.0&lt;/a&gt;, via Wikimedia Commons.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  When the agent must stop and ask
&lt;/h2&gt;

&lt;p&gt;Here's my take, and I expect some people to disagree with it: &lt;strong&gt;the agent should not ask clarifying questions because it feels unsure.&lt;/strong&gt; Model confidence is a bad trigger. It asks too often on easy questions and not at all on the ones that look easy and aren't, like "revenue last month."&lt;/p&gt;

&lt;p&gt;The trigger should be the spread. Run the plausible interpretations. If they all land within tolerance, answer and show the receipt. If they don't, ask, and ask about the specific fork that moved the number.&lt;/p&gt;

&lt;p&gt;And the tolerance isn't a model setting. It's a business requirement with an owner. Here's the decision table I'd put in the spec, first match wins:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;th&gt;Agent does&lt;/th&gt;
&lt;th&gt;Receipt shows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;A certified metric matches the question&lt;/td&gt;
&lt;td&gt;Answer with the certified definition&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;answer_type: certified&lt;/code&gt;, definition version, who certified it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;No certified metric; all interpretations within tolerance (Northstar: 1% for finance metrics, 5% for operational counts)&lt;/td&gt;
&lt;td&gt;Answer with the most common reading&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;answer_type: improvised&lt;/code&gt;, every default applied, the spread&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;No certified metric; spread above tolerance; the forks are known (status set, refund timing, timezone)&lt;/td&gt;
&lt;td&gt;Ask one question that names the fork and shows the numbers ("Gross: $7,000. Net of refunds: $5,550. Which do you mean?")&lt;/td&gt;
&lt;td&gt;The options and their values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Spread above tolerance and the agent can't name the fork&lt;/td&gt;
&lt;td&gt;Don't answer. Route to the data team&lt;/td&gt;
&lt;td&gt;The interpretations it tried and why it stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;The question touches a metric on the "certified only" list (revenue, margin, headcount) and rule 1 didn't match&lt;/td&gt;
&lt;td&gt;Don't improvise. Say the metric isn't certified for this question and who to ask&lt;/td&gt;
&lt;td&gt;The metric's owner&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Rule 5 is the one teams push back on, because it makes the agent answer fewer questions in month one. I'd still ship it. For a small set of numbers that end up in board decks, an agent that says "I can't answer that one yet" is doing its job. Every refusal there is also a free backlog item: the questions people ask that have no certified answer are exactly the ones your data team should certify next.&lt;/p&gt;
&lt;h2&gt;
  
  
  Acceptance criteria you can hand to the team
&lt;/h2&gt;

&lt;p&gt;Written in the Given/When/Then style from my &lt;a href="https://github.com/prachitparikh/ba-toolkit/blob/main/templates/acceptance-criteria-template.md" rel="noopener noreferrer"&gt;acceptance criteria template&lt;/a&gt;, using the fixture above as the test data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;Feature: Answer receipts for the data assistant

  Scenario: Certified metric answers with its definition
    Given the metric "net_revenue" v2 is certified by the finance reporting lead
    And the Northstar October fixture is loaded
    When a user in America/New_York asks "What was revenue last month?" on 2026-11-16
    Then the answer is 5550.00 USD
    And the receipt shows answer_type "certified", metric id "net_revenue" and version "v2"
    And the receipt shows the period in America/New_York with UTC bounds 2026-10-01T04:00Z to 2026-11-01T04:00Z
    And the receipt shows 8 orders before the status filter and 6 after

  Scenario: Material ambiguity triggers one specific question
    Given "net_revenue" is NOT certified
    And the tolerance for finance metrics is 1%
    When the same user asks the same question
    Then the agent does not return a single number
    And it asks one question naming the refund treatment, showing 7,000.00 and 5,550.00

  Scenario: Certified-only metric is never improvised
    Given "gross_margin" is on the certified-only list and has no certified definition
    When a user asks "What was gross margin last month?"
    Then the agent returns no number
    And it names the metric owner to contact

  Scenario: Offsetting edge rows are caught at row level
    Given the agent's answer and finance's answer are within 2% in total
    When the UAT check compares order IDs behind both answers
    Then any order ID present in one and not the other is listed as a defect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last scenario is the C-versus-D trap written as a test. A total-only check would pass it. An order-ID check doesn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I'd test the receipts themselves
&lt;/h2&gt;

&lt;p&gt;Receipts can lie too. An agent can write &lt;code&gt;"timezone": "America/New_York"&lt;/code&gt; and run the query in UTC. So the checks have to compare the receipt to what actually happened, not to itself.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Re-run the receipt's SQL&lt;/strong&gt; outside the agent and confirm the same number. If they differ, the receipt isn't describing the query that produced the answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-derive the period bounds&lt;/strong&gt; from the stated timezone and compare to the UTC bounds in the receipt. Do this on a daylight-saving month on purpose. November is perfect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recount the rows&lt;/strong&gt; at each stage and compare to &lt;code&gt;row_counts&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plant a cancelled order&lt;/strong&gt; in the fixture and check the receipt's status filter excludes it, and the count drops by one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask the same question three times.&lt;/strong&gt; The answer can stay the same while &lt;code&gt;defaults_applied&lt;/code&gt; changes, and that's a stability defect even if the number didn't move.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check &lt;code&gt;certified&lt;/code&gt; against the source of truth.&lt;/strong&gt; The certified list lives in the semantic model or verified-query repository, not in the agent's memory. A receipt that says certified for a metric that isn't in the repository is a severity-one defect.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How many runs is enough for that last kind of check? That's a separate question, and I wrote it up separately: &lt;a href="https://prachitparikh.hashnode.dev/ai-uat-sample-size" rel="noopener noreferrer"&gt;zero failures in 20 runs isn't a pass&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this doesn't solve
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It doesn't make a wrong certified metric right.&lt;/strong&gt; If finance's v2 definition is wrong, every certified answer is wrong with a nice receipt attached. Certification moves the risk to a person, which is where it belongs, but it doesn't remove it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The interpretation list can miss the real fork.&lt;/strong&gt; The agent can only compare the readings it thinks of. Northstar's fixture has five. A real warehouse with 869 columns, as BEAVER describes, has more forks than anyone will enumerate. That's why rule 4 exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receipts add latency and screen space.&lt;/strong&gt; Users will ignore a 30-line receipt. Show three things up front (certified or not, period with timezone, row count) and the rest behind a click.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I haven't benchmarked an agent here.&lt;/strong&gt; This is a spec and a fixture, not a measurement of any product. The numbers above are from my SQLite run on fictional data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd do on Monday
&lt;/h2&gt;

&lt;p&gt;If a text-to-SQL or "ask your data" pilot is on your roadmap, here's the order I'd work in.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick the five questions that end up in decks.&lt;/strong&gt; Revenue, margin, active customers, churn, headcount, whatever yours are. Put them on a certified-only list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write each as a definition with a grain sentence&lt;/strong&gt; ("one row = one order"), eligible statuses, the timezone, and what an empty period returns. My post on &lt;a href="https://prachitparikh.github.io/blog/metric-view-relationships.html" rel="noopener noreferrer"&gt;metric view relationships&lt;/a&gt; has the same discipline for semantic layers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load them as certified answers&lt;/strong&gt; in whatever your tool supports: verified answers in Power BI, a verified query repository in Snowflake, a named metric in your semantic layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a fixture like the one above&lt;/strong&gt; for each, with at least one edge row per fork (a cancelled order, a refund across the month boundary, an order near midnight UTC).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the tolerance per metric family&lt;/strong&gt; and get the owner's name next to it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add the receipt fields to the acceptance criteria&lt;/strong&gt; before the first sprint, not after the first wrong number in a deck.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Profile the source tables first.&lt;/strong&gt; The forks come from the data. My &lt;a href="https://prachitparikh.hashnode.dev/sql-data-profiling" rel="noopener noreferrer"&gt;SQL data profiling&lt;/a&gt; queries are where I'd start finding them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And if the agent is also writing the SQL that the receipts describe, &lt;a href="https://dev.to/prachitparikh/review-ai-generated-sql-before-you-trust-the-number-a-checklist-with-a-runnable-fixture-2j45"&gt;review it like any other code&lt;/a&gt; against a fixture with a known answer. The receipt tells you what it did. The review tells you whether that was right.&lt;/p&gt;

&lt;h2&gt;
  
  
  Over to you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If your team has a text-to-SQL or Copilot pilot running, does the user see whether an answer was certified or improvised?&lt;/li&gt;
&lt;li&gt;Who should own the tolerance: the data team, finance, or the person asking?&lt;/li&gt;
&lt;li&gt;Would you ship rule 5 (certified-only metrics) in month one, knowing the agent will say "I can't answer that" a lot?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'd especially like to hear from anyone who's put verified answers or a verified query repository into production. Which questions did you certify first? Drop it in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AI Explore, &lt;a href="https://dev.to/aiexplore369zoho/text-to-sql-agents-answer-questions-nobody-reviewed-add-receipts-123h"&gt;Text-to-SQL Agents Answer Questions Nobody Reviewed. Add Receipts&lt;/a&gt;, Dev.to, 13 November 2026.&lt;/li&gt;
&lt;li&gt;Microsoft Learn, &lt;a href="https://learn.microsoft.com/power-bi/create-reports/copilot-prepare-data-ai-verified-answers" rel="noopener noreferrer"&gt;Prepare your data for AI: Verified answers&lt;/a&gt;, last updated 10 September 2026.&lt;/li&gt;
&lt;li&gt;Snowflake Documentation, &lt;a href="https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst/verified-query-repository" rel="noopener noreferrer"&gt;Cortex Analyst Verified Query Repository&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Peter Baile Chen, Devin Yang, Weiyue Li, Fabian Wenz, Yi Zhang, Nesime Tatbul, Michael Cafarella, Çağatay Demiralp, Michael Stonebraker, &lt;a href="https://arxiv.org/abs/2409.02038" rel="noopener noreferrer"&gt;BEAVER: An Enterprise Benchmark for Text-to-SQL&lt;/a&gt;, arXiv, v3, 13 May 2026.&lt;/li&gt;
&lt;li&gt;Hilal Ud Din, &lt;a href="https://www.iiba.org/business-analysis-blogs/where-ai-helps-my-business-analysis-work-and-where-i-draw-the-line/" rel="noopener noreferrer"&gt;Where AI Helps My Business Analysis Work (And Where I Draw the Line)&lt;/a&gt;, IIBA Analyst Catalyst blog, 20 October 2026.&lt;/li&gt;
&lt;li&gt;Atlan, &lt;a href="https://atlan.com/know/ai-agent/data-for-ai/text-to-sql-with-ai/" rel="noopener noreferrer"&gt;Text-to-SQL with AI: How It Works and Where It Breaks&lt;/a&gt;, 19 August 2026 (a vendor page; used here only for its description of failure modes).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I'm Prachit Parikh, a business analyst. I turn what stakeholders ask for into requirements a team can test, check the data with SQL, and see changes through UAT. I keep free templates, including &lt;a href="https://github.com/prachitparikh/ba-toolkit/blob/main/templates/meeting-notes-to-user-stories-receipts.md" rel="noopener noreferrer"&gt;the receipts method for AI-written user stories&lt;/a&gt; and seven SQL validation checks, in my &lt;a href="https://github.com/prachitparikh/ba-toolkit" rel="noopener noreferrer"&gt;BA Toolkit&lt;/a&gt; on &lt;a href="https://github.com/prachitparikh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and I make short animated explainers on &lt;a href="https://www.youtube.com/@PrachitParikh1" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. Besides Dev.to, I write on &lt;a href="https://prachitparikh.substack.com" rel="noopener noreferrer"&gt;Substack&lt;/a&gt;, &lt;a href="https://medium.com/@prachit.parikh" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;, &lt;a href="https://prachitparikh.hashnode.dev" rel="noopener noreferrer"&gt;Hashnode&lt;/a&gt;, &lt;a href="https://prachitparikh-ba.blogspot.com" rel="noopener noreferrer"&gt;Blogger&lt;/a&gt; and &lt;a href="https://prachitparikhblog.wordpress.com" rel="noopener noreferrer"&gt;WordPress&lt;/a&gt;, and I answer questions on &lt;a href="https://www.quora.com/profile/Prachit-Parikh-2" rel="noopener noreferrer"&gt;Quora&lt;/a&gt;. You can also find me on &lt;a href="https://www.linkedin.com/in/prachitp" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://bsky.app/profile/prachitparikh.bsky.social" rel="noopener noreferrer"&gt;Bluesky&lt;/a&gt;, &lt;a href="https://www.kaggle.com/prachitparikh" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt;, &lt;a href="https://www.credly.com/users/prachit-parikh" rel="noopener noreferrer"&gt;Credly&lt;/a&gt;, &lt;a href="https://www.crunchbase.com/person/prachit-parikh-ct" rel="noopener noreferrer"&gt;Crunchbase&lt;/a&gt; and &lt;a href="https://gravatar.com/prachitparikhba" rel="noopener noreferrer"&gt;Gravatar&lt;/a&gt;. Everything is collected on &lt;a href="https://prachitparikh.github.io/" rel="noopener noreferrer"&gt;my website&lt;/a&gt;, &lt;a href="https://prachitparikh.github.io/portfolio.html" rel="noopener noreferrer"&gt;my portfolio&lt;/a&gt; and &lt;a href="https://sites.google.com/view/prachit-parikh" rel="noopener noreferrer"&gt;my profile hub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>ai</category>
      <category>dataengineering</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Review AI-generated SQL before you trust the number: a checklist with a runnable fixture</title>
      <dc:creator>Prachit Parikh</dc:creator>
      <pubDate>Thu, 05 Nov 2026 02:14:30 +0000</pubDate>
      <link>https://dev.to/prachitparikh/review-ai-generated-sql-before-you-trust-the-number-a-checklist-with-a-runnable-fixture-2j45</link>
      <guid>https://dev.to/prachitparikh/review-ai-generated-sql-before-you-trust-the-number-a-checklist-with-a-runnable-fixture-2j45</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1515879218367-8466d910aaa4%3Ffm%3Djpg%26q%3D60%26w%3D3000%26auto%3Dformat%26fit%3Dcrop" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1515879218367-8466d910aaa4%3Ffm%3Djpg%26q%3D60%26w%3D3000%26auto%3Dformat%26fit%3Dcrop" alt="A dark code editor with lines of colorful source code" width="799" height="534"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Photo by Chris Ried on &lt;a href="https://unsplash.com/photos/a-computer-screen-with-a-bunch-of-code-on-it-ieic5Tq8YMk" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; (Unsplash License).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A query that runs is not a query that's right. Most of us know this, and few of us act on it when an assistant hands back a query in four seconds and the number looks about right.&lt;/p&gt;

&lt;p&gt;This is the review I run on AI-written SQL before its number goes anywhere near a sign-off. Eight checks, each written as a query that returns rows only when something is wrong. It comes with a tiny fictional fixture you can paste into SQLite, where a perfectly plausible revenue query returns &lt;strong&gt;1,820&lt;/strong&gt; and the reviewed one returns &lt;strong&gt;825&lt;/strong&gt;. Same tables, same question, same month.&lt;/p&gt;

&lt;p&gt;Nothing in the bad query is exotic. That's sort of the point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Related:&lt;/strong&gt; a query can be correct and still answer a question nobody asked. When an AI agent writes the SQL for a business question, I want a receipt next to the number: &lt;a href="https://dev.to/prachitparikh/text-to-sql-answer-receipts-the-spec-id-write-before-an-ai-agent-answers-one-data-question-1jm2"&gt;text-to-SQL answer receipts, the spec I'd write first&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why the "it ran" signal got weaker, not stronger
&lt;/h2&gt;

&lt;p&gt;The text-to-SQL benchmarks look great right now, and they're worth reading closely before you lean on them.&lt;/p&gt;

&lt;p&gt;On the &lt;a href="https://bird-bench.github.io" rel="noopener noreferrer"&gt;BIRD leaderboard&lt;/a&gt;, the top test execution accuracy as of 22 October 2026 is 83.40% (AskData, from AT&amp;amp;T's CDO team), with GrainSQL at 82.95% and Huawei's DataGallery at 82.39%. The human baseline on the same page, from data engineers and database students, is 92.96%. So the best system still misses roughly one question in six, and that's on a curated benchmark with clean schemas.&lt;/p&gt;

&lt;p&gt;Numbers like these are easy to misread. &lt;a href="https://spider2-sql.github.io" rel="noopener noreferrer"&gt;Spider 2.0&lt;/a&gt; uses enterprise-style workflows on Snowflake and BigQuery, and its top Spider 2.0-Snow entries sit around 96.70%, while the top Spider 2.0-Lite score is 76.23%. Same family of tasks, very different numbers. The &lt;a href="https://github.com/xlang-ai/Spider2" rel="noopener noreferrer"&gt;Spider 2.0 repo&lt;/a&gt; also has a long changelog of evaluation fixes and ambiguity cleanups, and as of August 2026 a suspended Snowflake evaluation account was disrupting Snow access.&lt;/p&gt;

&lt;p&gt;Then there's the paper that made me stop quoting leaderboard numbers at all. In &lt;a href="https://arxiv.org/abs/2601.08778" rel="noopener noreferrer"&gt;Pervasive Annotation Errors Break Text-to-SQL Benchmarks and Leaderboards&lt;/a&gt; (Jin, Choi, Zhu and Kang, January 2026), expert review found annotation error rates of 52.8% in BIRD Mini-Dev and 62.8% in Spider 2.0-Snow. When they re-scored 16 open-source agents on a corrected BIRD Dev subset, relative performance moved by -7% to +31% and rankings shifted by up to nine places. BIRD has since shipped a &lt;a href="https://bird-bench.github.io" rel="noopener noreferrer"&gt;2026 Mini-Dev update with corrected annotations&lt;/a&gt; (28 October 2026), which is the right response. But think about what it means: the people grading the SQL got it wrong more than half the time on some subsets. The "gold" queries had the same class of mistakes I'm about to show you.&lt;/p&gt;

&lt;p&gt;The vendors say it plainly too. Google's &lt;a href="https://docs.cloud.google.com/bigquery/docs/write-sql-gemini" rel="noopener noreferrer"&gt;Gemini in BigQuery docs&lt;/a&gt; say the model "can generate output that seems plausible but is factually incorrect," recommend you "validate all output," and note it "might suggest different syntax each time that you enter the same prompt." That last line matters for review. If you regenerate, you're reviewing a new query.&lt;/p&gt;

&lt;p&gt;And developers know it. In the &lt;a href="https://survey.stackoverflow.co/2026/" rel="noopener noreferrer"&gt;Stack Overflow 2026 Developer Survey&lt;/a&gt;, the most common answer to "How much do you trust the output from AI tools or AI agents as part of your workflow?" was "I trust it when I can easily verify the output," at 48.0% of 14,304 respondents. Only 6.6% said they trust it for many tasks including important work decisions. (&lt;a href="https://stackoverflow.blog/2026/10/06/the-results-of-the-2026-developer-survey-are-here/" rel="noopener noreferrer"&gt;The survey announcement&lt;/a&gt; has the broader picture: more than 30,000 people took it.)&lt;/p&gt;

&lt;p&gt;A revenue number in a steering-committee deck is an important work decision. So for an analyst the question is very practical: what does "easily verify" look like for SQL?&lt;/p&gt;
&lt;h2&gt;
  
  
  The take you can argue with
&lt;/h2&gt;

&lt;p&gt;Here's my position. The person who reviews AI-written SQL before sign-off should be the person who owns the metric definition, not the strongest SQL writer in the room. On most teams that's the BA or the analyst, not the engineer.&lt;/p&gt;

&lt;p&gt;The reason is in the fixture below. None of the five bugs is a syntax problem. Every one is a definition problem: which statuses count, whose calendar, which customers, what one row means. An engineer who writes beautiful SQL but wasn't in the room when finance said "test accounts don't count" will approve the query, and they'll approve it faster if it looks tidy.&lt;/p&gt;

&lt;p&gt;You can disagree with me on this one. A fair counter is that BAs miss performance and correctness issues an engineer catches right away, like a NOT IN against a nullable column. True, and that's why the checklist includes those. But I'd still rather teach the definition owner eight mechanical checks than teach the strongest SQL writer every one of the business rules.&lt;/p&gt;
&lt;h2&gt;
  
  
  The fixture: Northstar Systems, October revenue
&lt;/h2&gt;

&lt;p&gt;Northstar Systems is a fictional B2B software company, and every row below is invented. Here's the question someone typed into an assistant:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What was Northstar's revenue from completed orders in October 2026, and how many orders?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before any SQL, write the definition down. A reader taught me this habit: name the grain first, "one row = one order line" (&lt;a href="https://dev.to/ontomato/comment/3joab"&gt;ontomato's comment&lt;/a&gt;). Shivam Pandit makes the same point at metric level in &lt;a href="https://dev.to/panditshivam/defining-revenue-for-an-sql-agent-one-row-at-a-time-a4h"&gt;Defining revenue for an SQL agent, one row at a time&lt;/a&gt;: a definition an agent can use has to state grain, statuses, join behaviour and what an empty result means.&lt;/p&gt;

&lt;p&gt;So here's Northstar's:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;Definition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Grain of the source&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;order_lines&lt;/code&gt;: one row = one order line&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Grain of the answer&lt;/td&gt;
&lt;td&gt;one row = one order, then one total&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Revenue&lt;/td&gt;
&lt;td&gt;sum of &lt;code&gt;qty * unit_price&lt;/code&gt; over lines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Statuses&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;completed&lt;/code&gt; only. Not refunded, pending or cancelled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Calendar&lt;/td&gt;
&lt;td&gt;October in America/New_York. Timestamps are stored in UTC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exclusions&lt;/td&gt;
&lt;td&gt;customers flagged &lt;code&gt;is_test = 1&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty month&lt;/td&gt;
&lt;td&gt;report 0 only if the month's data is loaded; otherwise "no data"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now the schema and data. It runs as-is in the &lt;code&gt;sqlite3&lt;/code&gt; shell or any SQLite client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Northstar Systems (fictional). All data invented.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_test&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at_utc&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payment_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;paid_at_utc&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;support_tickets&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticket_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opened_at_utc&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Alder Clinic'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Birch Freight'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Cedar Labs'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Dogwood Foods'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'QA Test Account'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-01 02:30:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-05 15:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-12 18:20:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1004&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-14 09:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1005&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'refunded'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-10-20 13:45:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-31 21:15:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1007&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-22 16:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1008&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-11-01 02:10:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1009&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="s1"&gt;'2026-10-25 11:30:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-BASE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-PRO'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-ADDON'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-PRO'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-ADDON'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-SETUP'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1004&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-BASE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1005&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-PRO'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-BASE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-ADDON'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1007&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-ENT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1008&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-BASE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1009&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'NS-PRO'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;110&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;paid_at_utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-01 02:31:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-05 15:02:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-12 18:25:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1003&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-26 10:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1005&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-20 13:50:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-31 21:16:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1007&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-22 16:01:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1008&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-11-01 02:12:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;support_tickets&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;opened_at_utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-03 14:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-15 10:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2026-10-18 09:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three rows are planted on purpose. Order 1001 is 2:30 a.m. UTC on 1 October, which is 10:30 p.m. on 30 September in New York. Order 1008 is the reverse: November in UTC, October in New York. Order 1003 was paid in two instalments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The query that looks fine
&lt;/h2&gt;

&lt;p&gt;This is an &lt;strong&gt;illustrative example I wrote myself&lt;/strong&gt;, in the shape I see assistants produce for this kind of prompt. I'm not claiming any specific tool wrote it. Read it once and note what you'd flag before scrolling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;revenue&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'cancelled'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at_utc&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'2026-10-01'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="s1"&gt;'2026-10-31'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks tidy, and the aliases are sensible. Joining payments even feels responsible, as if it's making sure we only count money that actually came in. Here's the output (SQLite 3.53.1, run through Python's &lt;code&gt;sqlite3&lt;/code&gt; module):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orders | revenue
5 | 1820
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The reviewed query
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;order_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;          &lt;span class="c1"&gt;-- grain: one row = one order&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_revenue&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*)                          AS orders,
       COALESCE(SUM(t.order_revenue), 0) AS revenue
FROM orders o
JOIN order_totals t ON t.order_id = o.order_id
JOIN customers c    ON c.customer_id = o.customer_id
WHERE o.status = 'completed'
  AND c.is_test = 0
  -- October in America/New_York (EDT, UTC-4 all month), half-open range
  AND o.created_at_utc &amp;gt;= '2026-10-01 04:00:00'
  AND o.created_at_utc &amp;lt;  '2026-11-01 04:00:00';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orders | revenue
4 | 825
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Side by side:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Illustrative AI-style query&lt;/th&gt;
&lt;th&gt;Reviewed query&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Orders&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Revenue&lt;/td&gt;
&lt;td&gt;1,820&lt;/td&gt;
&lt;td&gt;825&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Which orders&lt;/td&gt;
&lt;td&gt;1001, 1002, 1003 (counted twice), 1005, 1007&lt;/td&gt;
&lt;td&gt;1002, 1003, 1006, 1008&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The flawed number is 121% too high. Look at the order count, though: five versus four. If you sanity-checked the count against a CRM export, you'd shrug and approve it. &lt;code&gt;COUNT(DISTINCT ...)&lt;/code&gt; quietly absorbed the duplication that &lt;code&gt;SUM&lt;/code&gt; didn't.&lt;/p&gt;

&lt;p&gt;That's the pattern Shivam's post names: "Distinct values are not distinct orders." I'd add a corollary. A DISTINCT you didn't ask for is a confession that someone saw duplicates and chose to hide them instead of explaining them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the 995 went: a fix ladder
&lt;/h2&gt;

&lt;p&gt;One fix at a time, real output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;step | revenue | orders
1 flawed as written | 1820 | 5
2 drop payments join | 1530 | 6
3 status = completed | 1270 | 4
4 ET half-open range | 1325 | 5
5 exclude test accounts | 825 | 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2 is the interesting one. Removing the payments join takes out 400 of double-counted revenue from order 1003, but the order count goes &lt;em&gt;up&lt;/em&gt;. The inner join to payments had been silently dropping order 1009, the pending order with no payment. Two bugs were partly cancelling each other out. That happens more than you'd think, and it's why I don't trust "the total looks roughly right" as evidence of anything.&lt;/p&gt;

&lt;p&gt;Step 4 also goes up: the BETWEEN with bare dates excluded order 1006 at 9:15 p.m. UTC on 31 October (because &lt;code&gt;'2026-10-31 21:15:00'&lt;/code&gt; sorts after &lt;code&gt;'2026-10-31'&lt;/code&gt;), and the UTC calendar put 1008 in November and 1001 in October. Fixing the calendar removes one order and adds two.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1461749280684-dccba630e2f6%3Ffm%3Djpg%26q%3D60%26w%3D3000%26auto%3Dformat%26fit%3Dcrop" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1461749280684-dccba630e2f6%3Ffm%3Djpg%26q%3D60%26w%3D3000%26auto%3Dformat%26fit%3Dcrop" alt="A monitor showing code in an editor, slightly out of focus" width="799" height="534"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Photo by Ilya Pavlov on &lt;a href="https://unsplash.com/photos/monitor-showing-java-programming-OqtafYT5kTw" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; (Unsplash License).&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The checklist: 8 checks, each one a query that fails loudly
&lt;/h2&gt;

&lt;p&gt;Manuel Diaz left a comment on one of my &lt;a href="https://dev.to/prachitparikh"&gt;Dev.to posts&lt;/a&gt; that changed how I write these: make each check a query that returns rows only when something is wrong. Zero rows is a pass, and anything else is a conversation. Here's the review in that form.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Grain: does the join multiply rows?
&lt;/h3&gt;

&lt;p&gt;Ask: after all the joins, how many rows does each order have, compared with how many lines it really has?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS joined_rows,
       (SELECT COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;line_rows&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) &amp;gt; (SELECT COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order_id | joined_rows | line_rows
1003 | 6 | 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One row back means fail. Order 1003 has three lines and two payments, so the join gives six rows. The usual sources of fan-out are payments, shipments, status history, any table with "events" in its name, and slowly changing dimensions joined without a date condition. The fix is to aggregate to the answer's grain &lt;em&gt;before&lt;/em&gt; joining, which is exactly what the CTE in the reviewed query does.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Every inner join: what does it drop?
&lt;/h3&gt;

&lt;p&gt;Fan-out inflates; inner joins deflate. For each &lt;code&gt;JOIN&lt;/code&gt;, count the left-side rows that have no match. If the number isn't zero, someone has to decide whether those rows should be gone. The payments join dropped order 1009, which happened to be right for the wrong reason.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Status filters: list the values, don't guess them
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COALESCE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'&amp;lt;NULL&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n
FROM orders GROUP BY status ORDER BY n DESC;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;status | n
completed | 6
refunded | 1
pending | 1
cancelled | 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;status != 'cancelled'&lt;/code&gt; is an exclusion list, and exclusion lists rot. Every new status (&lt;code&gt;refunded&lt;/code&gt;, &lt;code&gt;on_hold&lt;/code&gt;, &lt;code&gt;chargeback&lt;/code&gt;) silently joins the revenue. Prefer an inclusion list, &lt;code&gt;status IN ('completed')&lt;/code&gt;, that maps to the written definition. Also note that &lt;code&gt;!=&lt;/code&gt; drops rows where status is NULL, which is a separate decision nobody made.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Date boundaries and time zones
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at_utc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="kt"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at_utc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-4 hours'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;created_at_et&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'%m'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at_utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'%m'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at_utc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-4 hours'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;created_at_utc&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'2026-10-31 %'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order_id | created_at_utc | created_at_et
1001 | 2026-10-01 02:30:00 | 2026-09-30 22:30:00
1006 | 2026-10-31 21:15:00 | 2026-10-31 17:15:00
1008 | 2026-11-01 02:10:00 | 2026-10-31 22:10:00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three rows, three questions. What zone are the timestamps stored in? What zone does the business mean by "October"? Does the range include the whole last day? Use half-open ranges (&lt;code&gt;&amp;gt;= start AND &amp;lt; next_start&lt;/code&gt;), never &lt;code&gt;BETWEEN&lt;/code&gt; on timestamps.&lt;/p&gt;

&lt;p&gt;Daylight saving time adds a trap, and this year it sits right on the boundary. US DST ended at 2 a.m. on Sunday, 1 November 2026, so midnight on 1 November in New York was still UTC-4, which is 04:00 UTC. Midnight on 1 December is UTC-5, 05:00 UTC. A hardcoded &lt;code&gt;-4 hours&lt;/code&gt; is only right for part of the year. In a warehouse, use its real time-zone conversion function (&lt;code&gt;CONVERT_TIMEZONE&lt;/code&gt;, &lt;code&gt;AT TIME ZONE&lt;/code&gt;, &lt;code&gt;DATETIME(ts, 'America/New_York')&lt;/code&gt;) and check what it does at the switch.&lt;/p&gt;

&lt;p&gt;Alex Shev's comment on boundary records is the habit to copy here (&lt;a href="https://dev.to/alexshev/comment/3jng3"&gt;comment&lt;/a&gt;): put a row a minute either side of every boundary into your test data, deliberately.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. NULLs in aggregates and in NOT IN
&lt;/h3&gt;

&lt;p&gt;Two different problems, both quiet.&lt;/p&gt;

&lt;p&gt;Aggregates skip NULLs, and &lt;code&gt;COUNT(*)&lt;/code&gt; doesn't:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS all_rows, COUNT(customer_id) AS non_null_ids,
       COUNT(DISTINCT customer_id) AS distinct_ids
FROM support_tickets;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;all_rows | non_null_ids | distinct_ids
3 | 2 | 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If an assistant writes &lt;code&gt;AVG(discount)&lt;/code&gt; and a NULL discount means "no discount," the average covers discounted rows only. That might be the definition, but usually it's not. Ask what NULL means in that column, then choose between &lt;code&gt;AVG(x)&lt;/code&gt; and &lt;code&gt;AVG(COALESCE(x, 0))&lt;/code&gt; on purpose.&lt;/p&gt;

&lt;p&gt;NOT IN is worse. Ask for "customers with no support tickets":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;support_tickets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customer_id | name
(0 rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero. A single ticket with a NULL &lt;code&gt;customer_id&lt;/code&gt; makes &lt;code&gt;NOT IN&lt;/code&gt; evaluate to unknown for every customer, so nothing comes back, and there's no error. The same question with &lt;code&gt;NOT EXISTS&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;support_tickets&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customer_id | name
2 | Birch Freight
4 | Dogwood Foods
5 | QA Test Account
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And notice the test account turned up again. Every check leaks into the others.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. The empty set: what should "nothing" return?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;raw_sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;COALESCE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;coalesced&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS rows_matched
FROM orders o JOIN order_lines l ON l.order_id=o.order_id
WHERE o.created_at_utc &amp;gt;= '2026-12-01 05:00:00' AND o.created_at_utc &amp;lt; '2027-01-01 05:00:00';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;raw_sum | coalesced | rows_matched
NULL | 0 | 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SUM&lt;/code&gt; over nothing is NULL. Assistants often wrap it in &lt;code&gt;COALESCE(..., 0)&lt;/code&gt; because a 0 looks friendlier on a dashboard. But "zero revenue in December" and "December hasn't loaded yet" are different statements, and the coalesced query can't tell them apart. My rule: return the row count next to the total, and let the definition decide what an empty month displays. I used &lt;code&gt;COALESCE&lt;/code&gt; in the reviewed query above because Northstar's definition says October is loaded. That's a claim you should check with a control total from the source system, not assume.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Silent DISTINCT
&lt;/h3&gt;

&lt;p&gt;Search the query for &lt;code&gt;DISTINCT&lt;/code&gt;. For each one, ask what duplicate it was hiding and why that duplicate exists. If nobody can answer, remove the DISTINCT and run check 1. In the fixture, &lt;code&gt;COUNT(DISTINCT o.order_id)&lt;/code&gt; gave a believable order count on top of a revenue figure that was more than double.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT DISTINCT *&lt;/code&gt; at the top of a query and &lt;code&gt;UNION&lt;/code&gt; (instead of &lt;code&gt;UNION ALL&lt;/code&gt;) belong in the same bucket. Both deduplicate without telling you what got merged.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Hallucinated columns, and the SQLite trap
&lt;/h3&gt;

&lt;p&gt;Assistants invent plausible column names: &lt;code&gt;region&lt;/code&gt;, &lt;code&gt;order_total&lt;/code&gt;, &lt;code&gt;is_active&lt;/code&gt;. Usually the database throws an error and you're fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: no such column: region
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wrap that invented name in double quotes, as some generators do by habit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"region"&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order_id | "region"
1001 | region
1002 | region
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error. In this SQLite build, a double-quoted identifier that doesn't match a column falls back to a string literal, so every row gets the word "region." Group by it and you get one tidy bucket. Whether this happens depends on how your SQLite build is compiled and configured (look up its "double-quoted string literals" setting); the library build I ran here allows it. Other engines may behave differently, but the habit still matters: for every column the query uses, confirm it exists in the schema and means what the query assumes. A column that exists but means something else, like &lt;code&gt;created_at&lt;/code&gt; versus &lt;code&gt;booked_at&lt;/code&gt;, is the harder version to catch.&lt;/p&gt;

&lt;h2&gt;
  
  
  The review card
&lt;/h2&gt;

&lt;p&gt;If you want one thing to copy, it's this. Paste it into the PR description or the ticket next to any AI-drafted query.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;Pass looks like&lt;/th&gt;
&lt;th&gt;Ask the AI or the author&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;Definition written&lt;/td&gt;
&lt;td&gt;Grain, statuses, calendar, exclusions, empty-set rule are on paper&lt;/td&gt;
&lt;td&gt;"Which line of the definition does each WHERE clause implement?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Grain / fan-out&lt;/td&gt;
&lt;td&gt;Grain check returns 0 rows&lt;/td&gt;
&lt;td&gt;"What is one row at each step?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Inner joins&lt;/td&gt;
&lt;td&gt;Dropped-row count is 0 or explained&lt;/td&gt;
&lt;td&gt;"Which rows does this join remove?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Status filter&lt;/td&gt;
&lt;td&gt;Inclusion list matching the definition&lt;/td&gt;
&lt;td&gt;"List every status value and say why it's in or out."&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Dates and zones&lt;/td&gt;
&lt;td&gt;Half-open range, stated zone, boundary rows checked&lt;/td&gt;
&lt;td&gt;"What happens to a row at 11:59 p.m. on the last day?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;NULLs&lt;/td&gt;
&lt;td&gt;NULL meaning stated; NOT EXISTS instead of NOT IN&lt;/td&gt;
&lt;td&gt;"Which columns in this query can be NULL?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Empty set&lt;/td&gt;
&lt;td&gt;Row count returned with total&lt;/td&gt;
&lt;td&gt;"What does this return for a month with no data?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;DISTINCT / UNION&lt;/td&gt;
&lt;td&gt;Each one justified&lt;/td&gt;
&lt;td&gt;"What duplicate is this removing?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Columns exist and mean it&lt;/td&gt;
&lt;td&gt;Every column matched to the data dictionary&lt;/td&gt;
&lt;td&gt;"Show me where this column is defined."&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Control total&lt;/td&gt;
&lt;td&gt;Matches the source system within an agreed tolerance&lt;/td&gt;
&lt;td&gt;(not a question for the AI)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Row 9 isn't SQL review in the strict sense. It's Alex Shev's control-total habit, and it's the only check that catches a query that's internally consistent and still wrong because the data underneath is incomplete.&lt;/p&gt;

&lt;p&gt;I keep a longer set of these as copy-paste queries in &lt;a href="https://github.com/prachitparikh/ba-toolkit/blob/main/sql/data-validation-checks.sql" rel="noopener noreferrer"&gt;data-validation-checks.sql&lt;/a&gt; in my BA Toolkit, and the reconciliation walk-through is in &lt;a href="https://github.com/prachitparikh/ba-toolkit/tree/main/examples/revenue-reconciliation" rel="noopener noreferrer"&gt;examples/revenue-reconciliation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the sharpest commenter will say
&lt;/h2&gt;

&lt;p&gt;"Just give the model the schema and the definition and it'll get it right." Partly. Giving an assistant the definition table above would probably fix the status and test-account bugs. It's the single best thing you can do on the generation side, and it's what the r/LLMDevs commenters meant by a semantic layer. But it doesn't remove the review. The definition can be incomplete (ours says nothing about NULL statuses), and Google's own docs warn the same prompt can give different SQL each time. You'd be reviewing a better first draft, which is nice, not skipping the review.&lt;/p&gt;

&lt;p&gt;"Ask the AI to review its own query." I do this, and it does catch things. I treat it like a linter: helpful for the mechanical items (NOT IN, BETWEEN on timestamps), weak on definition items, because the model doesn't know that finance excludes test accounts unless you tell it, and if you told it, it should have used that the first time.&lt;/p&gt;

&lt;p&gt;"Benchmarks say 83%, so it's fine most of the time." Benchmarks score whether the result set matches a reference answer. The annotation-error paper suggests the references themselves were wrong a lot of the time. And your schema isn't a benchmark schema. A per-query review is the only number that applies to your query.&lt;/p&gt;

&lt;p&gt;When to rerun the review: whenever the query is regenerated, the source schema changes, a new status value appears, the reporting period crosses a DST change or year end, or the definition changes. A reviewed query is reviewed against a definition and a snapshot of data. Change either and the review expires.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I didn't test
&lt;/h2&gt;

&lt;p&gt;Honest limits of this fixture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's SQLite, with timestamps stored as text. Warehouses store real timestamp types, and time-zone functions differ between engines. Check 4's &lt;code&gt;-4 hours&lt;/code&gt; is a teaching shortcut, not a production pattern.&lt;/li&gt;
&lt;li&gt;There's no NULL status row in the data, so I haven't shown &lt;code&gt;!=&lt;/code&gt; dropping one. Add &lt;code&gt;(1010, 2, NULL, '2026-10-15 12:00:00')&lt;/code&gt; to the orders and see what each query does.&lt;/li&gt;
&lt;li&gt;No currency, no tax, no partial refunds, no line-level discounts. Real revenue definitions have all of these, and each one is another grain question.&lt;/li&gt;
&lt;li&gt;I didn't time anything. Some fixes (pre-aggregating in a CTE) change query plans on large tables.&lt;/li&gt;
&lt;li&gt;I wrote the flawed query. It's realistic, but it's mine, so it's a teaching example, not evidence about how often any assistant makes these mistakes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd do on Monday
&lt;/h2&gt;

&lt;p&gt;If you're reviewing AI-drafted SQL starting this week, here's the order I'd go in.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Before opening the query&lt;/strong&gt;, write the definition table: grain, statuses, calendar and zone, exclusions, empty-set rule. Five lines. If you can't fill it in, the problem isn't the SQL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a ten-row fixture&lt;/strong&gt; with one boundary row on each side of the period, one order with two payments, one test account, one NULL in a key you filter on. Fifteen minutes. Reuse it for every query on that metric.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run the grain check and the status list&lt;/strong&gt; first. They catch the most, fastest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ban &lt;code&gt;BETWEEN&lt;/code&gt; on timestamps and &lt;code&gt;NOT IN&lt;/code&gt; on subqueries&lt;/strong&gt; in your team's review notes. These two are pure mechanics and easy to agree on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return the row count with every total.&lt;/strong&gt; It costs one column and answers the empty-set question for free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Get one control total&lt;/strong&gt; from the source system for the period and write down the tolerance you accept.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save the reviewed query with its definition&lt;/strong&gt; next to it, so the next regeneration has something to be compared against.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you're on the product side setting up an AI analytics tool, my &lt;a href="https://github.com/prachitparikh/ba-toolkit/blob/main/templates/ai-agent-test-plan.md" rel="noopener noreferrer"&gt;AI agent test plan template&lt;/a&gt; is built for the next step: a golden set of questions with known answers, run more than once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch: finding the gap when two numbers don't match
&lt;/h2&gt;

&lt;p&gt;This is my short animated explainer on reconciling two numbers that should agree. It walks through definition, calendar and time zone, grain and fan-out, and orphan rows, which are most of the same failure modes from the reconciliation side.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/SG-6rtZeizg" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Video: "Dashboard Numbers Don't Match? Find the Gap in SQL" by Prachit Parikh, on &lt;a href="https://www.youtube.com/@PrachitParikh1" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the testing side of AI tools in general (golden sets, running each case three times, flagging unstable answers), there's &lt;a href="https://www.youtube.com/watch?v=IBteCgiXc6Q" rel="noopener noreferrer"&gt;AI Agent Testing for Business Analysts: Dev, UAT, Prod&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discussion
&lt;/h2&gt;

&lt;p&gt;A few questions I'd like real answers to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Who signs off on AI-drafted SQL on your team: the person who wrote the prompt, an engineer, or the metric owner? Has that ever gone wrong?&lt;/li&gt;
&lt;li&gt;What's a bug you've caught in generated SQL that isn't on this list? I suspect window functions with the wrong &lt;code&gt;PARTITION BY&lt;/code&gt; belong here.&lt;/li&gt;
&lt;li&gt;Should an empty period return 0, NULL, or no row at all? I've seen teams argue all three.&lt;/li&gt;
&lt;li&gt;Has giving the assistant a written metric definition measurably reduced the fixes you make?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Comment prompt:&lt;/strong&gt; paste the one-line check you'd add to the review card, written as a query that returns rows only when something's wrong. I'll test the good ones against the Northstar fixture and post the output in reply.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;BIRD benchmark leaderboard and news (top test EX 83.40%, human 92.96%, Mini-Dev 2026 update): &lt;a href="https://bird-bench.github.io" rel="noopener noreferrer"&gt;https://bird-bench.github.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Spider 2.0 leaderboard (Snow and Lite top scores): &lt;a href="https://spider2-sql.github.io" rel="noopener noreferrer"&gt;https://spider2-sql.github.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Spider 2.0 GitHub repository and changelog: &lt;a href="https://github.com/xlang-ai/Spider2" rel="noopener noreferrer"&gt;https://github.com/xlang-ai/Spider2&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Jin, Choi, Zhu, Kang, "Pervasive Annotation Errors Break Text-to-SQL Benchmarks and Leaderboards," arXiv:2601.08778, January 2026: &lt;a href="https://arxiv.org/abs/2601.08778" rel="noopener noreferrer"&gt;https://arxiv.org/abs/2601.08778&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Google Cloud, "Write queries with Gemini assistance" (BigQuery): &lt;a href="https://docs.cloud.google.com/bigquery/docs/write-sql-gemini" rel="noopener noreferrer"&gt;https://docs.cloud.google.com/bigquery/docs/write-sql-gemini&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Stack Overflow 2026 Developer Survey, full results: &lt;a href="https://survey.stackoverflow.co/2026/" rel="noopener noreferrer"&gt;https://survey.stackoverflow.co/2026/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Stack Overflow blog, "The results of the 2026 Developer Survey are here," 6 October 2026: &lt;a href="https://stackoverflow.blog/2026/10/06/the-results-of-the-2026-developer-survey-are-here/" rel="noopener noreferrer"&gt;https://stackoverflow.blog/2026/10/06/the-results-of-the-2026-developer-survey-are-here/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reader lessons credited above: &lt;a href="https://dev.to/ontomato/comment/3joab"&gt;ontomato&lt;/a&gt;, &lt;a href="https://dev.to/panditshivam/defining-revenue-for-an-sql-agent-one-row-at-a-time-a4h"&gt;Shivam Pandit&lt;/a&gt;, &lt;a href="https://dev.to/alexshev/comment/3jng3"&gt;Alex Shev&lt;/a&gt;, and Manuel Diaz.&lt;/p&gt;

&lt;p&gt;Related posts of mine: &lt;a href="https://dev.to/prachitparikh/7-sql-checks-i-run-before-uat-sign-off-with-queries-you-can-copy-38h5"&gt;7 SQL checks I run before UAT sign-off&lt;/a&gt;, &lt;a href="https://prachitparikh.hashnode.dev/data-mapping-document-template" rel="noopener noreferrer"&gt;Data mapping document template&lt;/a&gt;, and &lt;a href="https://medium.com/@prachit.parikh/ai-agents-can-write-the-code-now-the-bottleneck-is-the-requirements-5f7a1ee86362" rel="noopener noreferrer"&gt;AI agents can write the code now. The bottleneck is the requirements.&lt;/a&gt; More of my work is on my &lt;a href="https://prachitparikh.github.io/portfolio.html" rel="noopener noreferrer"&gt;portfolio page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was created with the help of AI. Every query and output above was run against the Northstar fixture in SQLite and checked before publishing.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I'm Prachit Parikh, a business analyst. I turn what stakeholders ask for into requirements a team can test, check the data with SQL, and see changes through UAT. I keep free templates, including the &lt;a href="https://github.com/prachitparikh/ba-toolkit/blob/main/sql/data-validation-checks.sql" rel="noopener noreferrer"&gt;SQL data validation checks&lt;/a&gt; behind this review card, in my &lt;a href="https://github.com/prachitparikh/ba-toolkit" rel="noopener noreferrer"&gt;BA Toolkit&lt;/a&gt; on &lt;a href="https://github.com/prachitparikh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and I make short animated explainers on &lt;a href="https://www.youtube.com/@PrachitParikh1" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. Besides Dev.to, I write on &lt;a href="https://prachitparikh.substack.com" rel="noopener noreferrer"&gt;Substack&lt;/a&gt;, &lt;a href="https://medium.com/@prachit.parikh" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;, &lt;a href="https://prachitparikh.hashnode.dev" rel="noopener noreferrer"&gt;Hashnode&lt;/a&gt;, &lt;a href="https://prachitparikh-ba.blogspot.com" rel="noopener noreferrer"&gt;Blogger&lt;/a&gt; and &lt;a href="https://prachitparikhblog.wordpress.com" rel="noopener noreferrer"&gt;WordPress&lt;/a&gt;, and I answer questions on &lt;a href="https://www.quora.com/profile/Prachit-Parikh-2" rel="noopener noreferrer"&gt;Quora&lt;/a&gt;. You can also find me on &lt;a href="https://www.linkedin.com/in/prachitp" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://bsky.app/profile/prachitparikh.bsky.social" rel="noopener noreferrer"&gt;Bluesky&lt;/a&gt;, &lt;a href="https://www.kaggle.com/prachitparikh" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt;, &lt;a href="https://www.credly.com/users/prachit-parikh" rel="noopener noreferrer"&gt;Credly&lt;/a&gt;, &lt;a href="https://www.crunchbase.com/person/prachit-parikh-ct" rel="noopener noreferrer"&gt;Crunchbase&lt;/a&gt; and &lt;a href="https://gravatar.com/prachitparikhba" rel="noopener noreferrer"&gt;Gravatar&lt;/a&gt;. Everything is collected on &lt;a href="https://prachitparikh.github.io/" rel="noopener noreferrer"&gt;my website&lt;/a&gt; and &lt;a href="https://sites.google.com/view/prachit-parikh" rel="noopener noreferrer"&gt;my profile hub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>ai</category>
      <category>database</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>7 SQL checks I run before UAT sign-off (with queries you can copy)</title>
      <dc:creator>Prachit Parikh</dc:creator>
      <pubDate>Mon, 02 Nov 2026 02:45:46 +0000</pubDate>
      <link>https://dev.to/prachitparikh/7-sql-checks-i-run-before-uat-sign-off-with-queries-you-can-copy-38h5</link>
      <guid>https://dev.to/prachitparikh/7-sql-checks-i-run-before-uat-sign-off-with-queries-you-can-copy-38h5</guid>
      <description>&lt;p&gt;The hour I trust least on any project is the one right before UAT sign-off. The testers have clicked through every screen, the defect board is mostly green, and someone is waiting for a signature. Screens can look perfect while the data underneath is wrong, though, and nobody finds out until the first month-end report goes to finance.&lt;/p&gt;

&lt;p&gt;So before I sign anything as the business analyst, I run a short set of SQL checks against the test environment. It takes about an hour. The checks are built for the problems that weeks of manual testing tend to walk right past: a load that silently drops a slice of rows, a currency field that doubles on one branch of the data, customers who have orders but no customer record.&lt;/p&gt;

&lt;p&gt;Here are the seven checks, with generic queries you can adapt. The table names are made up, so swap in your own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefer to watch?&lt;/strong&gt; In my video &lt;a href="https://www.youtube.com/watch?v=SG-6rtZeizg" rel="noopener noreferrer"&gt;Dashboard Numbers Don't Match? Find the Gap in SQL&lt;/a&gt;, I reconcile two revenue numbers in SQL step by step: the definition, the calendar, the grain of the joins, and orphan rows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Related:&lt;/strong&gt; If an AI assistant drafted the query, run it through &lt;a href="https://dev.to/prachitparikh/review-ai-generated-sql-before-you-trust-the-number-a-checklist-with-a-runnable-fixture-2j45"&gt;my 8-check review for AI-generated SQL&lt;/a&gt; before you trust the number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a business analyst should write SQL at all
&lt;/h2&gt;

&lt;p&gt;You don't need to be a data engineer. You need to be able to answer one question without waiting on anyone: "Is the data in the new system what the business expects?" Developers test that the code does what the spec says. A BA's job is to check that the spec, and the data it produces, matches what the business needs. SQL is the fastest way to do that yourself.&lt;/p&gt;

&lt;p&gt;If you can write &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;GROUP BY&lt;/code&gt;, &lt;code&gt;JOIN&lt;/code&gt; and &lt;code&gt;CASE&lt;/code&gt;, you can run every check below.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Row counts: did everything arrive?
&lt;/h2&gt;

&lt;p&gt;Start with the dumbest possible question. If the source had 48,210 orders for a day, does the target have 48,210?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'source'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS row_count
FROM staging.orders
WHERE load_date = '2026-10-30'
UNION ALL
SELECT 'target', COUNT(*)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fact_orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;load_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-10-30'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the numbers differ, ask why before anything else. Sometimes there's a good reason (test orders filtered out on purpose). Often there isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Totals that should tie out
&lt;/h2&gt;

&lt;p&gt;Counts can match while amounts don't. Sum the money by a period the business cares about and compare.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;DATE_TRUNC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'month'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fact_orders&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the same query on the source or the legacy report. Put the two side by side in Excel with a difference column. Any month that doesn't tie to the cent gets a defect.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Duplicates on the business key
&lt;/h2&gt;

&lt;p&gt;Duplicates are the classic silent killer. One order counted twice inflates revenue and nobody notices until an auditor does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS copies
FROM dw.fact_orders
GROUP BY order_number
HAVING COUNT(*)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;copies&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the key the business uses, not the surrogate ID the database made up. An order number, invoice number or account number is what people will actually reconcile against.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Orphans: records pointing at nothing
&lt;/h2&gt;

&lt;p&gt;Every order should belong to a customer that exists. Every transaction should belong to an account that exists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fact_orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;dw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dim_customer&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
       &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Orphans usually mean the loads ran in the wrong order or a lookup failed. Reports built on an inner join will quietly drop these rows, which is how totals go missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Nulls in fields the business depends on
&lt;/h2&gt;

&lt;p&gt;Go back to the requirements. Which fields did stakeholders say were mandatory? Check them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS total_rows,
       SUM(CASE WHEN region IS NULL THEN 1 ELSE 0 END) AS missing_region,
       SUM(CASE WHEN product_code IS NULL THEN 1 ELSE 0 END) AS missing_product,
       SUM(CASE WHEN order_amount IS NULL THEN 1 ELSE 0 END) AS missing_amount
FROM dw.fact_orders;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A dashboard filtered by region will hide every row with a null region. That's a requirement failure even if no screen ever throws an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Values outside the allowed list
&lt;/h2&gt;

&lt;p&gt;If the requirement says status can only be Open, Shipped, Cancelled or Returned, prove it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS rows_with_status
FROM dw.fact_orders
WHERE status NOT IN ('Open', 'Shipped', 'Cancelled', 'Returned')
   OR status IS NULL
GROUP BY status;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one finds mapping gaps fast. A legacy code like "SHP" that nobody mapped will show up here instead of in a confused user's email three weeks after go-live.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Trace five real records end to end
&lt;/h2&gt;

&lt;p&gt;Not every check is a query. Pick five records the business knows well: a big customer, a refund, an order that changed status twice, something from the last day of a month. Follow each one from the source screen, through the tables, to the final report. Write down what you see at every step.&lt;/p&gt;

&lt;p&gt;Queries tell you the data is consistent. Tracing real records tells you it means what people think it means.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I log the results
&lt;/h2&gt;

&lt;p&gt;I keep one simple table per test cycle:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;Query saved as&lt;/th&gt;
&lt;th&gt;Expected&lt;/th&gt;
&lt;th&gt;Actual&lt;/th&gt;
&lt;th&gt;Pass/Fail&lt;/th&gt;
&lt;th&gt;Defect&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Row count 30 Oct&lt;/td&gt;
&lt;td&gt;rc_orders.sql&lt;/td&gt;
&lt;td&gt;48,210&lt;/td&gt;
&lt;td&gt;48,210&lt;/td&gt;
&lt;td&gt;Pass&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Duplicates&lt;/td&gt;
&lt;td&gt;dup_orders.sql&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;Fail&lt;/td&gt;
&lt;td&gt;DEF-212&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each row links back to a requirement in the traceability matrix, so when someone asks "did we test that?", the answer is one click away. It also makes the sign-off conversation short: here's what we checked, here's what passed, here are the open defects and their severity.&lt;/p&gt;

&lt;h2&gt;
  
  
  When a check fails
&lt;/h2&gt;

&lt;p&gt;Don't sign off with a known data defect and a promise to fix it later. Raise it with the exact query attached, the expected result, and the actual result. Developers can rerun it in seconds, and nobody has to argue about what "the numbers look off" means.&lt;/p&gt;

&lt;p&gt;Then agree on severity with the business owner, not alone. A 14-row duplicate in a test table might be minor. A 14-row duplicate in a revenue report is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one-page version
&lt;/h2&gt;

&lt;p&gt;Before you sign UAT, confirm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Row counts match between source and target for at least two load dates&lt;/li&gt;
&lt;li&gt;Money totals tie out by month&lt;/li&gt;
&lt;li&gt;No duplicates on the business key&lt;/li&gt;
&lt;li&gt;No orphan records on any key relationship&lt;/li&gt;
&lt;li&gt;Mandatory fields have no nulls&lt;/li&gt;
&lt;li&gt;Coded fields only contain allowed values&lt;/li&gt;
&lt;li&gt;Five real records traced end to end&lt;/li&gt;
&lt;li&gt;Every failure has a defect with the query attached&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's an hour of work that protects weeks of everyone else's.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I'm Prachit Parikh, a business analyst. I turn what stakeholders ask for into requirements a team can test, check the data with SQL, and see changes through UAT. I keep free templates, including these SQL checks, in my &lt;a href="https://github.com/prachitparikh/ba-toolkit" rel="noopener noreferrer"&gt;BA Toolkit&lt;/a&gt; on &lt;a href="https://github.com/prachitparikh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and I make short animated explainers on &lt;a href="https://www.youtube.com/@PrachitParikh1" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. Besides Dev.to, I write on &lt;a href="https://prachitparikh.substack.com" rel="noopener noreferrer"&gt;Substack&lt;/a&gt;, &lt;a href="https://medium.com/@prachit.parikh" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;, &lt;a href="https://prachitparikh.hashnode.dev" rel="noopener noreferrer"&gt;Hashnode&lt;/a&gt;, &lt;a href="https://prachitparikh-ba.blogspot.com" rel="noopener noreferrer"&gt;Blogger&lt;/a&gt; and &lt;a href="https://prachitparikhblog.wordpress.com" rel="noopener noreferrer"&gt;WordPress&lt;/a&gt;, and I answer questions on &lt;a href="https://www.quora.com/profile/Prachit-Parikh-2" rel="noopener noreferrer"&gt;Quora&lt;/a&gt;. You can also find me on &lt;a href="https://www.linkedin.com/in/prachitp" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://bsky.app/profile/prachitparikh.bsky.social" rel="noopener noreferrer"&gt;Bluesky&lt;/a&gt;, &lt;a href="https://www.kaggle.com/prachitparikh" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt;, &lt;a href="https://www.credly.com/users/prachit-parikh" rel="noopener noreferrer"&gt;Credly&lt;/a&gt;, &lt;a href="https://www.crunchbase.com/person/prachit-parikh-ct" rel="noopener noreferrer"&gt;Crunchbase&lt;/a&gt; and &lt;a href="https://gravatar.com/prachitparikhba" rel="noopener noreferrer"&gt;Gravatar&lt;/a&gt;. Everything is collected on &lt;a href="https://prachitparikh.github.io/" rel="noopener noreferrer"&gt;my website&lt;/a&gt; and &lt;a href="https://sites.google.com/view/prachit-parikh" rel="noopener noreferrer"&gt;my profile hub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What check would you add to this list? I'd like to hear the one that saved you on a project.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>testing</category>
      <category>database</category>
      <category>career</category>
    </item>
  </channel>
</rss>
