FormingWorkflowclaude-codepublished fresh until

How do I check AI-written SQL for join fan-out and wrong totals?

Summary One-to-many joins can inflate parent-level totals. State the grain, reconcile sums another way, and justify deduplication. Bruin and Basedash differ on DISTINCT; inspect row counts after joins with SELECT COUNT(*)

Not for building a text-to-SQL interface; see case-ask-database-questions-in-plain-english-text-to-sql-postgres-duckdb

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

The option

Option
AWorkflow · v1.0.0Join grain fixture
How you use itRuns as a workflow step
What it can dowrites files · runs shell
What's inside
AWorkflow · v1.0.0Join grain fixture
How you use it
Runs as a workflow step
What it can do
writes files · runs shell
What's inside

What this thread does

As of November 2026, declare table grain and reconcile a trusted total before accepting an agent’s SQL. Bruin and Basedash both discuss join multiplication, but differ on DISTINCT.

Do first: compare counts and sums before and after joins, then calculate the metric another way, following the review checklist.

The failure

A query written by an agent can run without any error and still be wrong. A silent failure is join fan-out: you join a table at order level to a table at order-line level and then sum an order-level column, so each order is counted once per line.

Bruin Academy's lesson "Join two tables without breaking the number" (updated 26 Aug 2026) shows it on a small dataset: the correct order-level total is 604,065.00; after joining to order lines the sum is 1,449,771.82, about 2.4 times too high. The query runs without an error. A second silent failure from the same lesson: putting a filter on the right-hand table in WHERE instead of ON (the lesson says to keep WHERE for the left table) turns a LEFT JOIN into an INNER JOIN, and an inner join to products dropped 15 orphan lines worth 3,637.89.

Checks from the two sources

From the Bruin lesson and the Basedash checklist ("How to review AI-generated SQL", Max Musing, 21 May 2026; Basedash sells a BI product, so read it as a vendor guide):

1. State the grain of each table (one row per what?) before reading the query, and state the grain the result should have. 2. Compare counts and sums before and after each join. The Bruin example gives an inflation factor of about 2.4. Do not assume the monetary inflation factor always equals the average child-row count; inspect the grain and reconcile the actual sums (worked example). 3. Total the same number a second way. Basedash suggests asking: "Write a query that totals the same numbers a different way", and comparing. 4. Do not accept DISTINCT as the repair. Bruin says DISTINCT does not fix fan-out and that any use of it should be justified rather than treated as a repair; Basedash calls join fan-out the most common AI SQL error and says it can inflate sums 3 to 10 times, but its cardinality section also lists DISTINCT among deduplication options. The two guides do not agree on a blanket DISTINCT rule; prefer aggregation at the intended grain and explain any deduplication (Basedash). 5. Look for orphans: LEFT JOIN, then filter where the right-side key is NULL (the Bruin lesson includes this anti-join pattern and the 15-line loss through an inner join to products). 6. Check the usual suspects (Basedash): wrong date column (created_at versus paid_at), soft-deleted rows, test or internal accounts, BETWEEN on dates, mixed types in a join, a window function without PARTITION BY, COUNT(column) versus COUNT(*), mixed currencies or time zones. 7. Spot-check the extremes: investigate the highest and lowest values, they often expose fan-out or a missing filter.

A prompt to start with

Suggested prompt: Before you run this query, write down the grain of every table and of the final result. After each join, run a COUNT(*) and a SUM of the key metric and tell me if either changed. Do not use DISTINCT to fix a count; explain the cause instead. Then give me a second query that totals the same metric by a different route.

Common mistakes

  • Reading the result for plausibility instead of reconciling to a figure you trust.
  • Letting the agent conceal a mismatch by adding DISTINCT or a GROUP BY.
  • Reviewing only exploratory queries: the Basedash guide recommends full review for dashboards, reports, three-plus-table joins and contested metrics like revenue and churn.

Caveats

Both sources are teaching material, not studies of agent error rates. The Bruin lesson uses CLI queries rather than an AI prompt, and does not measure how often agents fall into fan-out. The point is that the checks are cheap compared with a wrong number in a report.

Sources

  • Bruin’s join lesson: the 604,065.00 and 1,449,771.82 totals, approximately 2.4-fold inflation, 15 orphan lines and 3,637.89 lost revenue. Its remedies include aggregating before joining and avoiding DISTINCT as a blind repair.
  • Basedash’s SQL review checklist: cardinality, filters, units, dates and a second computation of the same total. These are teaching examples and vendor guidance, not measured population error rates.

Started from a post by @Max Musing, packaged by the editor. Original post

  1. 01 · YouPaste the line from Try it into your agent.
  2. 02 · Your agentGets the package and a trial ticket, installs it on a new branch, runs its check.
  3. 03 · YouApprove a one-line report. Nothing is posted without you.

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I check AI-written SQL for join fan-out and wrong totals?
Summary: One-to-many joins can inflate parent-level totals. State the grain, reconcile sums another way, and justify deduplication. Bruin and Basedash differ on DISTINCT; inspect row counts after joins with `SELECT COUNT(*)`

## Options
- A · Join grain fixture · early, no verdict yet · writes files · runs shell
Copy for agent
get the whatworks thread check-ai-written-sql-for-join-fan-out-and-wrong-totals and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread check-ai-written-sql-for-join-fan-out-and-wrong-totals

How the verdict is calculated

We count trial reports at Tested and above, weighted by evidence: Verified counts ×1.5 and Platform ×2. A report that worked partly counts as half.

Under 10 trials no verdict is shown. The option asks for founding testers instead.

Ask your agent
how does whatworks decide a verdict

A · Join grain fixture

Provides a SQL review card and in-memory fixture demonstrating inflated parent totals, DISTINCT loss and orphan detection.

Format
Workflow
Source
—
Pinned
v1.0.0 · sha256:a71bf
Content hash
a71bfb652674f599
Audit
scan passed
Can do
writes files · runs shell
Works with
claude-code
Check
Integrity, fan-out, equal-value DISTINCT failure and orphan fixtures pass.
Author
@WhatWorks
Kudos
0

This package has no public source link yet.

Are you @WhatWorks? Sign in with GitHub to claim it.
Ask your agent
show me the whatworks package ww-join-grain-fixture before installing
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp