Messages-Per-User Lift and Pooled t-Statistic in SQL

Quick Overview

A SQL exercise on A/B test analysis: from a table of messages sent per user in control and test groups, compute both group means, the percentage lift and the pooled-variance two-sample t-statistic in a single query. It tests conditional aggregation, sample variance and numeric arithmetic in PostgreSQL.

Messages-Per-User Lift and Pooled t-Statistic in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `user_activity` has one row per user in an A/B test, with the user's group (`'control'` or `'test'`) and the number of messages the user sent. Compare mean messages per user between the groups and return one row with: - `control_mean` and `test_mean`, rounded to 4 decimals - `lift_pct` = `(test_mean - control_mean) / control_mean * 100`, rounded to 2 decimals - `t_statistic`: the pooled-variance two-sample t-statistic, with `'control'` as group 1 and `'test'` as group 2, rounded to 4 decimals ```hint Per-group building blocks Every output can be built from three numbers per group: the count, the mean and the sample variance (denominator `n - 1`). All six can come from a single scan of the table. ``` ### Input Table(s) Table `user_activity` (primary key `user_id`): | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(20) | Experiment group: `'control'` or `'test'` | | `user_id` | INT | User identifier, one row per user | | `messages_sent` | INT | Number of messages the user sent | ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | Rounding | |---|---|---| | `control_mean` | Mean of `messages_sent` over control users | 4 decimals | | `test_mean` | Mean of `messages_sent` over test users | 4 decimals | | `lift_pct` | `(test_mean - control_mean) / control_mean * 100` | 2 decimals | | `t_statistic` | Pooled-variance two-sample t-statistic, control as group 1 | 4 decimals | With $n_1$, $\bar{x}_1$, $s_1^2$ the size, mean and sample variance of the control group and $n_2$, $\bar{x}_2$, $s_2^2$ those of the test group: $$ s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}, \qquad t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_p^2\left(\frac{1}{n_1} + \frac{1}{n_2}\right)}} $$ Compute `lift_pct` and `t_statistic` from the unrounded means and variances, and round only the final values. The t-statistic is negative when the test mean is higher. The result is a single row, so no ordering is needed. ### Example Input `user_activity`: | group_name | user_id | messages_sent | |---|---|---| | control | 17700 | 14 | | control | 87612 | 19 | | test | 73703 | 10 | | control | 33938 | 11 | | control | 78888 | 13 | | test | 54647 | 24 | | test | 38845 | 18 | | test | 6460 | 18 | | test | 4289 | 25 | | control | 77892 | 21 | | control | 40067 | 12 | | test | 37321 | 25 | | control | 89163 | 5 | | test | 45115 | 18 | | control | 69759 | 6 | | test | 51421 | 19 | Expected output: | control_mean | test_mean | lift_pct | t_statistic | |---|---|---|---| | 12.6250 | 19.6250 | 55.45 | -2.6332 | The eight control users sent 101 messages in total (mean 12.625) and the eight test users sent 157 (mean 19.625), so the lift is 7 / 12.625 × 100 = 55.45. The sample variances are 31.125 and about 25.4107, the pooled variance is 395.75 / 14 ≈ 28.2679, the standard error is $\sqrt{28.2679 \times 0.25} \approx 2.6584$, and the t-statistic is -7 / 2.6584 ≈ -2.6332. ### Constraints and Clarifications - `user_id` is unique, so each row is one user and nothing needs deduplicating. - `group_name` is always `'control'` or `'test'`, and `messages_sent` is a non-negative integer that is never NULL. - Each group has at least two users, the control mean is greater than 0, and the pooled variance is greater than 0, so every output is defined. - Variances are sample variances, with denominator `n - 1`.

Overview: A SQL exercise on A/B test analysis: from a table of messages sent per user in control and test groups, compute both group means, the percentage lift and the pooled-variance two-sample t-statistic in a single query. It tests conditional aggregation, sample variance and numeric arithmetic in PostgreSQL.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Table user_activity has one row per user in an A/B test, with the user's group ('control' or 'test') and the number of messages the user sent. Compare mean messages per user between the groups and return one row with:

  • control_mean and test_mean , rounded to 4 decimals
  • lift_pct = (test_mean - control_mean) / control_mean * 100 , rounded to 2 decimals
  • t_statistic : the pooled-variance two-sample t-statistic, with 'control' as group 1 and 'test' as group 2, rounded to 4 decimals

Input Table(s)

Table user_activity (primary key user_id):

ColumnTypeMeaning
group_nameVARCHAR(20)Experiment group: 'control' or 'test'
user_idINTUser identifier, one row per user
messages_sentINTNumber of messages the user sent

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinitionRounding
control_meanMean of messages_sent over control users4 decimals
test_meanMean of messages_sent over test users4 decimals
lift_pct(test_mean - control_mean) / control_mean * 1002 decimals
t_statisticPooled-variance two-sample t-statistic, control as group 14 decimals

With n1n_1, xˉ1\bar{x}_1, s12s_1^2 the size, mean and sample variance of the control group and n2n_2, xˉ2\bar{x}_2, s22s_2^2 those of the test group:

sp2=(n1−1)s12+(n2−1)s22n1+n2−2,t=xˉ1−xˉ2sp2(1n1+1n2)s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}, \qquad t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_p^2\left(\frac{1}{n_1} + \frac{1}{n_2}\right)}}

Compute lift_pct and t_statistic from the unrounded means and variances, and round only the final values. The t-statistic is negative when the test mean is higher. The result is a single row, so no ordering is needed.

Example

Input user_activity:

group_nameuser_idmessages_sent
control1770014
control8761219
test7370310
control3393811
control7888813
test5464724
test3884518
test646018
test428925
control7789221
control4006712
test3732125
control891635
test4511518
control697596
test5142119

Expected output:

control_meantest_meanlift_pctt_statistic
12.625019.625055.45-2.6332

The eight control users sent 101 messages in total (mean 12.625) and the eight test users sent 157 (mean 19.625), so the lift is 7 / 12.625 × 100 = 55.45. The sample variances are 31.125 and about 25.4107, the pooled variance is 395.75 / 14 ≈ 28.2679, the standard error is 28.2679×0.25≈2.6584\sqrt{28.2679 \times 0.25} \approx 2.6584, and the t-statistic is -7 / 2.6584 ≈ -2.6332.

Constraints and Clarifications

  • user_id is unique, so each row is one user and nothing needs deduplicating.
  • group_name is always 'control' or 'test' , and messages_sent is a non-negative integer that is never NULL.
  • Each group has at least two users, the control mean is greater than 0, and the pooled variance is greater than 0, so every output is defined.
  • Variances are sample variances, with denominator n - 1 .