Your A/B Test Passed. Now Prove It Wasn't Measuring the Wrong People: SQL Prompts That Hunt Sampling Errors Before You Trust the Uplift

A query runs clean, the number looks plausible, the uplift is +3.2%, and the team ships the feature. Two weeks later someone notices the control group had 4% more returning users than the treatment — because a JOIN dropped anonymous sessions on one side only. The metric was real. The conclusion was noise.

This is the failure mode LLMs are genuinely good at catching, and almost nobody points them at it. Ask a model "is my A/B test valid?" and you get a polite paragraph about p-values. Ask it to walk through the sampling logic step by step — JOINs, filters, deduplication, denominator, observation window — and it will find the session that appears in both arms, the user who entered the test three days late, and the metric computed over 14 days for one group and 13 for the other.

The prompts below are ordered the way the errors actually hide: from the population you think you selected, down to the number you finally print. Run them against your SQL before you run them against your stakeholders.

First, the question that decides everything: who is actually in your sample?

Every A/B test has two populations that are never quite the same: the one you assigned (everyone who hit the experiment flag) and the one you measured (everyone who survived your JOINs and WHERE clauses). The gap between them is where sampling errors live. More on the topic: “Green Tests, Broken Contracts: 12 LLM Prompts That Hunt Reentrancy, Oracle Holes, and Gas Leaks Before Your Solidity Audit”.

A practical way to see the gap: count distinct users at three points — at assignment, at first exposure event, and at metric calculation. If those three numbers diverge by more than a fraction of a percent, you do not have a test, you have two different studies glued together. More on the topic: “AI Tester (AI Auto-Tests): What Lies Behind Test Automation and Where It Breaks”.

Prompt 1 — reconstruct the funnel from assignment to measurement.

You are a data analyst reviewing an A/B test query. I will paste the SQL.

Do not evaluate statistical significance and do not comment on the metric value.

Your only job: reconstruct how many distinct users exist at each stage.

Produce a table with these stages and, for each, the exact line of SQL

responsible for including or excluding users:

1. Users present in the assignment/exposure table (the intended population)
2. Users surviving every JOIN in the metric query
3. Users surviving every WHERE filter
4. Users with a non-null metric value
5. Users reaching the final GROUP BY

For each stage, name the join type (INNER, LEFT) and the filter predicate

that could remove rows, and state which arm it removes rows from more often.

If a filter is asymmetric between control and treatment, flag it explicitly.

SQL:

<paste here>

Usage example: paste a query with JOIN sessions s ON s.user_id = u.id and the model will point out that INNER JOIN silently drops users with zero sessions — and those users are not distributed evenly across arms, because the treatment may have changed session-creation behavior. That is not a sampling detail. That is the treatment changing the denominator.

The denominator is a product decision, not a SQL detail If your treatment affects whether a user generates an event at all, an INNER JOIN removes them from the treatment arm's denominator but keeps them in control's. You are no longer comparing conversion — you are comparing conversion among people who converted enough to appear.

The JOIN that quietly rewrites your control group

INNER JOINs are the single most common source of sampling error in experiment SQL, and they are invisible because the query does not fail. It just returns fewer rows than you expected, and nobody counts.

Prompt 2 — audit every JOIN for row multiplication and row loss.

Review the following A/B test SQL as a reviewer looking for JOIN defects.

For every JOIN in the query, answer three questions in a table:

| JOIN | Type | Can it duplicate rows? | Can it drop users? | Which arm is affected more? |

Rules for your analysis:

- A JOIN on a table with multiple rows per user (events, sessions, orders)
  multiplies rows unless it is pre-aggregated or DISTINCT is applied.

  Show the exact multiplication factor if the cardinality is inferable.

- A JOIN to a dimension table (users, segments) can drop rows if the key
  is missing — this is common for users created after the dimension snapshot.

- Flag any JOIN whose ON clause includes a date or status filter: those
  filters apply at join time and can remove one arm's rows only.

Do not suggest optimizations. Only report what the JOIN does to the population.

Usage example: a query joining orders to compute revenue per user will multiply users with 6 orders into 6 rows. If you then average revenue per row instead of per user, the average is weighted by purchase frequency — and the arm with more repeat buyers wins for reasons that have nothing to do with the feature.

Deduplication: the bug that makes your test look twice as good

The same user can enter the exposure table multiple times — a page reload, a retry, a device switch, a backfill that re-inserted yesterday's partition. Each duplicate is a second vote in your average.

Prompt 3 — find where a user can appear in more than one arm or more than once.

I need to know whether any user can be counted more than once in this

experiment query, or appear in both the control and treatment arms.

Write diagnostic SQL (PostgreSQL dialect) that returns:

1. Users appearing in more than one variant in the exposure table
2. Users with duplicate exposure rows for the same variant
3. The count of affected users and the share of the total population

Then tell me which of these two deduplication strategies is correct for

this specific query and why: taking the FIRST exposure per user

(MIN(timestamp)) or the LAST (MAX(timestamp)). Base the answer on what the

metric measures — if the metric counts events after exposure, first exposure

is usually correct; explain when it is not.

Schema: <paste table DDL>

Query: <paste metric SQL>

A real pattern: a user hits the experiment flag on Monday as control, clears cookies, and lands in treatment on Wednesday. Without a first-exposure rule, they contribute to both arms and pull the two averages toward each other — the test looks conservative when it is actually contaminated.

Fix the arm before you fix the metric Deduplicate exposure first, materialize the assignment into a table with one row per user, and only then compute the metric. Recomputing the metric on a contaminated assignment table will not save you.

The observation window is a variable, not a constant

Here is a failure that survives every JOIN check: treatment users entered the test on day 1, control users trickled in over the first week, and you measured both over "the last 14 days." Treatment users simply had more days to convert.

Same 14-day window, different exposure — and the uplift flips
Fixed calendar window
  • Control: entered day 1, ~13 days observed
  • Treatment: entered day 6, ~9 days observed
  • Treatment has 3 fewer days to convert
  • Result: control wins on time, not on the feature
Per-user window from first exposure
  • Both arms observed for the same 14 days
  • Late entrants are not penalized
  • Uplift reflects the feature only
  • Result: the number you can actually defend

Prompt 4 — verify the window is identical for both arms.

Check whether this A/B test query measures both arms over the same

observation window. Answer these questions with the exact SQL lines:

1. Is the window defined relative to a fixed calendar date or relative to
   each user's exposure timestamp? Show where.

2. If it is a fixed calendar window, does every user in the sample have
   exposure before the window starts? If not, users who entered late are

   measured over a shorter period.

3. Are the two arms' exposure distributions comparable in time? Write a
   diagnostic query that returns, per arm, the median days between exposure

   and the end of the window.

4. If the distributions differ, state how the metric is biased and in which
   direction.

Do not propose a fix yet — only diagnose.

Usage example: a common output is "control median 13.1 days of observation, treatment 9.4 days." That is the whole story. Any conversion metric will favor control, because control users had three more days to do the thing you are counting.

The denominator is where a plausible number becomes a wrong one

Once the population is clean, the metric still has to divide by something. And the wrong denominator is the error that looks most convincing, because the resulting number is in a believable range.

Prompt 5 — interrogate the denominator choice.

For the metric below, list every reasonable denominator and state what each

one measures. Then tell me which one this SQL uses and what it actually

answers, not what it is named.

Metric name as written in the dashboard: <name>

SQL: <paste>

For each denominator option, give:

- what population it covers
- when it is the right choice for an A/B test
- how a treatment that changes user behavior would distort it

Finally: is the denominator computed before or after the treatment could

have affected it? If the treatment can change who is in the denominator,

say so plainly.

This is where LLMs earn their keep. A model will happily tell you that dividing conversions by users who opened the app during the test is not a conversion rate but a conditional rate — and that if the feature encourages app opens, the treatment arm's denominator grows with the numerator, and the metric moves for a reason you did not intend to test.

Now — and only now — compute the uplift

After population, deduplication, window, and denominator checks pass, the uplift calculation is the easy part. The point of ordering it last is that an uplift computed on a broken sample is not a weaker result, it is a different question's answer.

Prompt 6 — compute uplift with the sample caveats attached.

Compute the uplift for this experiment and report it with the sample

caveats we have already established.

Output format:

- Control: n users, metric value, denominator definition
- Treatment: n users, metric value, denominator definition
- Absolute and relative uplift
- A one-line statement of what population this uplift applies to
  (e.g. "users who had at least one session in the window")

- A list of every sampling assumption that must hold for this number
  to be trustworthy

Do not compute a p-value or confidence interval unless I ask. If the sample

sizes or the denominators differ between arms by more than 1%, stop and

tell me before computing anything.

That last instruction is the useful one. It turns the model from a calculator into a gate — and a mismatch between arm sizes is the cheapest early warning you will ever get.

When this approach works — and when it does not

These prompts are not a statistics engine. They do not replace a proper significance test, and they will not catch a metric that is correctly computed but conceptually wrong for your hypothesis. More on the topic: “QA Engineer: Test Design, Manual Testing, and Automation — How to Start from Scratch Without Stepping on Rakes”.

Your situation Does this help? What to do
Complex multi-JOIN metric SQL Yes — this is the core case Run prompts 1, 2, 3 in order
Simple single-table count Partially Prompt 5 (denominator) is still worth it
Sequential or switchback tests Weakly The window logic differs; check exposure timing manually
Metric defined in a BI tool, not SQL No You need the underlying query first
Very small samples No Sampling errors hide under noise; fix the query, then worry about power

The prompts also assume you can see the SQL. If the metric lives behind a dashboard you cannot inspect, the first task is not prompting — it is getting the query. A number you cannot audit is a number you cannot trust, no matter how plausible it looks. More on the topic: “Audit Your Own Query Log Before Choosing BM25 or Embeddings for RAG”.

If the statistical side is where your uncertainty actually sits — significance, peeking, multiple comparisons — that is a different problem with its own prompts. More on the topic: “A/B Testing That Survives Scrutiny: Prompts That Make an LLM Your Statistician, Not Your Cheerleader”.

And if your metric SQL is slow enough that you are tempted to skip the audit because it takes 20 minutes to run, fix the query first. More on the topic: “SQL on Steroids: 13 Prompts That Make LLMs Write Window Functions, Fix Slow Queries, and Explain PostgreSQL Plans”.

What to do on Monday

Before your next experiment reads out, paste its SQL into prompt 1 and count the users at each stage. If the numbers at assignment and at measurement differ by more than a rounding error, you have found your bug before it found your roadmap.

The habit worth building is not "ask the LLM if the test is valid" — that question is too vague to get a useful answer. The habit is asking it to reconstruct the population, step by step, and to name the line of SQL that lets each user in or out. A test you can explain line by line is a test you can defend in a review. Everything else is a plausible number.

AssetMaterials for this article

A document from this article — download and apply it.

  • XLSXAssignment-to-Measurement Funnel Audit Sheet.xlsxXLSX · 8.8 KBDownload
← All posts

Ask AI about this article

Ask a question about this article — ASI Biont will answer based on its text and the attached files.