Compute A/B test conversion rates with absolute and relative lift in SQL

Quick Overview

Write a SQL query that turns a per-user A/B test table into one row with the control and treatment conversion rates, the absolute lift and the relative lift in percent. It tests conditional aggregation, decimal division, rounding order and guarding against a zero control rate.

Compute A/B test conversion rates with absolute and relative lift in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `events` has one row per user in an A/B test. `variant` says which arm the user was in (`'A'` is control, `'B'` is treatment) and `converted` says whether the user converted (1 or 0). Write a query that returns a single row comparing the two variants: each variant's conversion rate, the absolute lift, and the relative lift as a percentage. ### Input Table(s) `events` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(5) | Experiment arm: `'A'` for control, `'B'` for treatment | | converted | SMALLINT | 1 if the user converted, 0 if not | Each row is one user. The table has no user id column, so there is no declared key; every row counts as a distinct user. ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | Rounding | |---|---|---| | rate_control | conversions / users for variant `'A'` | 4 decimals | | rate_treatment | conversions / users for variant `'B'` | 4 decimals | | abs_lift | rate_treatment - rate_control | 4 decimals | | rel_lift_pct | abs_lift / rate_control * 100 | 2 decimals | Compute `abs_lift` and `rel_lift_pct` from the unrounded rates and round only the final values. The result has a single row, so no ordering is needed. ### Example Input `events`: | variant | converted | |---|---| | B | 0 | | B | 0 | | B | 0 | | A | 0 | | A | 1 | | B | 0 | | B | 1 | | B | 0 | | A | 0 | | B | 1 | | A | 0 | | A | 0 | | A | 0 | | B | 1 | | B | 0 | | A | 0 | | A | 1 | | A | 0 | Expected result: | rate_control | rate_treatment | abs_lift | rel_lift_pct | |---|---|---|---| | 0.2222 | 0.3333 | 0.1111 | 50.00 | Control has 9 users and 2 conversions, so its rate is 2/9 = 0.2222. Treatment has 9 users and 3 conversions, so its rate is 3/9 = 0.3333. The absolute lift is 1/9 = 0.1111, and the relative lift is (1/9) / (2/9) * 100 = 50.00. ### Constraints and Clarifications - `variant` only takes the values `'A'` and `'B'`, and each variant has at least one row. - `converted` is never NULL and is always 0 or 1. - Every row is a different user, so there is nothing to deduplicate. - If the control conversion rate is 0, the relative lift is undefined. Return NULL for `rel_lift_pct` in that case; the query must not fail with a division-by-zero error. - The rates must be decimal values. Dividing one integer count by another truncates to 0 in PostgreSQL. ```hint Shape of the result The output is one row, not one row per variant. Think about how to get both arms' counts side by side in a single pass over the table. ```

Overview: Write a SQL query that turns a per-user A/B test table into one row with the control and treatment conversion rates, the absolute lift and the relative lift in percent. It tests conditional aggregation, decimal division, rounding order and guarding against a zero control rate.

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

Table events has one row per user in an A/B test. variant says which arm the user was in ('A' is control, 'B' is treatment) and converted says whether the user converted (1 or 0). Write a query that returns a single row comparing the two variants: each variant's conversion rate, the absolute lift, and the relative lift as a percentage.

Input Table(s)

events

ColumnTypeMeaning
variantVARCHAR(5)Experiment arm: 'A' for control, 'B' for treatment
convertedSMALLINT1 if the user converted, 0 if not

Each row is one user. The table has no user id column, so there is no declared key; every row counts as a distinct user.

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinitionRounding
rate_controlconversions / users for variant 'A'4 decimals
rate_treatmentconversions / users for variant 'B'4 decimals
abs_liftrate_treatment - rate_control4 decimals
rel_lift_pctabs_lift / rate_control * 1002 decimals

Compute abs_lift and rel_lift_pct from the unrounded rates and round only the final values. The result has a single row, so no ordering is needed.

Example

Input events:

variantconverted
B0
B0
B0
A0
A1
B0
B1
B0
A0
B1
A0
A0
A0
B1
B0
A0
A1
A0

Expected result:

rate_controlrate_treatmentabs_liftrel_lift_pct
0.22220.33330.111150.00

Control has 9 users and 2 conversions, so its rate is 2/9 = 0.2222. Treatment has 9 users and 3 conversions, so its rate is 3/9 = 0.3333. The absolute lift is 1/9 = 0.1111, and the relative lift is (1/9) / (2/9) * 100 = 50.00.

Constraints and Clarifications

  • variant only takes the values 'A' and 'B' , and each variant has at least one row.
  • converted is never NULL and is always 0 or 1.
  • Every row is a different user, so there is nothing to deduplicate.
  • If the control conversion rate is 0, the relative lift is undefined. Return NULL for rel_lift_pct in that case; the query must not fail with a division-by-zero error.
  • The rates must be decimal values. Dividing one integer count by another truncates to 0 in PostgreSQL.