Build an A/B test contingency table of conversions per variant in SQL

Quick Overview

Write a SQL query that turns one-row-per-visitor A/B test data into a contingency table with converted and not converted counts for each variant. It tests conditional aggregation with SUM over CASE, grouping, deterministic ordering and returning zero when a variant has no rows for an outcome.

Build an A/B test contingency table of conversions per variant in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `ab_events` table has one row per visitor with `variant` (`'control'` or `'treatment'`) and `outcome` (`'converted'` or `'not_converted'`). Using conditional aggregation, return one row per `variant` with `variant`, the count of `converted` visitors as `converted`, and the count of non-`converted` visitors as `not_converted`. ```hint Two columns from one pass Both counts come from the same group of rows. Ask what each row should contribute to each output column, and what adding up those contributions gives you. ``` ### Input Table(s) `ab_events` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(20) | Experiment group of the visitor: `'control'` or `'treatment'` | | outcome | VARCHAR(20) | Result for the visitor: `'converted'` or `'not_converted'` | The table has no key column. Each row is one visitor. ### Output Contract - Columns, in this order: `variant`, `converted`, `not_converted`. - `converted` is the number of the variant's rows with `outcome = 'converted'`; `not_converted` is the number of its rows with `outcome = 'not_converted'`. - Both counts are integers. - One row per variant that appears in `ab_events`, ordered by `variant` ascending. ### Example Input `ab_events`: | variant | outcome | |---|---| | treatment | not_converted | | treatment | not_converted | | treatment | converted | | treatment | converted | | control | not_converted | | control | not_converted | | treatment | converted | | control | converted | | control | not_converted | | control | converted | | control | not_converted | | control | converted | | treatment | not_converted | | treatment | converted | | treatment | converted | | control | not_converted | Expected output: | variant | converted | not_converted | |---|---|---| | control | 3 | 5 | | treatment | 5 | 3 | Each variant has 8 visitors. Treatment has 5 conversions and control has 3. ### Constraints and Clarifications - `variant` and `outcome` are never NULL, and the values are lowercase exactly as listed, so every row lands in exactly one of the two count columns. - A variant whose visitors all share one outcome still gets a row, with 0 in the other column. - Duplicate rows are separate visitors and each one counts.

Overview: Write a SQL query that turns one-row-per-visitor A/B test data into a contingency table with converted and not converted counts for each variant. It tests conditional aggregation with SUM over CASE, grouping, deterministic ordering and returning zero when a variant has no rows for an outcome.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The ab_events table has one row per visitor with variant ('control' or 'treatment') and outcome ('converted' or 'not_converted'). Using conditional aggregation, return one row per variant with variant, the count of converted visitors as converted, and the count of non-converted visitors as not_converted.

Input Table(s)

ab_events

ColumnTypeMeaning
variantVARCHAR(20)Experiment group of the visitor: 'control' or 'treatment'
outcomeVARCHAR(20)Result for the visitor: 'converted' or 'not_converted'

The table has no key column. Each row is one visitor.

Output Contract

  • Columns, in this order: variant , converted , not_converted .
  • converted is the number of the variant's rows with outcome = 'converted' ; not_converted is the number of its rows with outcome = 'not_converted' .
  • Both counts are integers.
  • One row per variant that appears in ab_events , ordered by variant ascending.

Example

Input ab_events:

variantoutcome
treatmentnot_converted
treatmentnot_converted
treatmentconverted
treatmentconverted
controlnot_converted
controlnot_converted
treatmentconverted
controlconverted
controlnot_converted
controlconverted
controlnot_converted
controlconverted
treatmentnot_converted
treatmentconverted
treatmentconverted
controlnot_converted

Expected output:

variantconvertednot_converted
control35
treatment53

Each variant has 8 visitors. Treatment has 5 conversions and control has 3.

Constraints and Clarifications

  • variant and outcome are never NULL, and the values are lowercase exactly as listed, so every row lands in exactly one of the two count columns.
  • A variant whose visitors all share one outcome still gets a row, with 0 in the other column.
  • Duplicate rows are separate visitors and each one counts.