Compute within-variant conversion proportions for an A/B test in SQL

Quick Overview

Write a SQL query that computes, for each A/B test variant, the share of visitors who converted and the share who did not, rounded to four decimals. It tests conditional aggregation, avoiding integer division, rounding numeric values in PostgreSQL and telling a within-group distribution apart from a joint one.

Compute within-variant conversion proportions for an A/B test in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `ab_events` has one row per visitor with `variant` (`'control'` or `'treatment'`) and `outcome` (`'converted'` or `'not_converted'`). For each `variant`, return `variant`, `prop_converted` and `prop_not_converted` (the conditional distribution of `outcome` within the variant), each rounded to 4 decimals. The two proportions in a row must sum to 1. ```hint Watch the division Check what your database returns when it divides one integer count by another. ``` ### 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`, `prop_converted`, `prop_not_converted`. - `prop_converted` is the number of the variant's rows with `outcome = 'converted'` divided by the variant's total row count. `prop_not_converted` is the same for `'not_converted'`. - Each proportion is rounded to 4 decimals on its own, with PostgreSQL `ROUND` on a numeric value. - One row per variant that appears in `ab_events`, ordered by `variant` ascending. ### Example Input `ab_events`: | variant | outcome | |---|---| | control | not_converted | | treatment | converted | | control | converted | | control | not_converted | | control | converted | | control | not_converted | | treatment | not_converted | | treatment | not_converted | | treatment | converted | | treatment | not_converted | | control | not_converted | | treatment | converted | | control | converted | | treatment | converted | Expected output: | variant | prop_converted | prop_not_converted | |---|---|---| | control | 0.4286 | 0.5714 | | treatment | 0.5714 | 0.4286 | Each variant has 7 visitors. Control converts 3 of 7 (0.428571...) and treatment converts 4 of 7 (0.571428...). ### Constraints and Clarifications - `variant` and `outcome` are never NULL, and the values are lowercase exactly as listed, so every row counts toward exactly one of the two proportions. - Every variant in the output has at least one row, so the denominator is never zero. - The two proportions are complementary: before rounding they sum to exactly 1. - Duplicate rows are separate visitors and each one counts.

Overview: Write a SQL query that computes, for each A/B test variant, the share of visitors who converted and the share who did not, rounded to four decimals. It tests conditional aggregation, avoiding integer division, rounding numeric values in PostgreSQL and telling a within-group distribution apart from a joint one.

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

Table ab_events has one row per visitor with variant ('control' or 'treatment') and outcome ('converted' or 'not_converted'). For each variant, return variant, prop_converted and prop_not_converted (the conditional distribution of outcome within the variant), each rounded to 4 decimals. The two proportions in a row must sum to 1.

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 , prop_converted , prop_not_converted .
  • prop_converted is the number of the variant's rows with outcome = 'converted' divided by the variant's total row count. prop_not_converted is the same for 'not_converted' .
  • Each proportion is rounded to 4 decimals on its own, with PostgreSQL ROUND on a numeric value.
  • One row per variant that appears in ab_events , ordered by variant ascending.

Example

Input ab_events:

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

Expected output:

variantprop_convertedprop_not_converted
control0.42860.5714
treatment0.57140.4286

Each variant has 7 visitors. Control converts 3 of 7 (0.428571...) and treatment converts 4 of 7 (0.571428...).

Constraints and Clarifications

  • variant and outcome are never NULL, and the values are lowercase exactly as listed, so every row counts toward exactly one of the two proportions.
  • Every variant in the output has at least one row, so the denominator is never zero.
  • The two proportions are complementary: before rounding they sum to exactly 1.
  • Duplicate rows are separate visitors and each one counts.