Two-Tailed P-Value for a Two-Proportion Z-Test from a Normal-CDF Lookup

Quick Overview

Write a SQL query that computes a pooled two-proportion z-statistic for an A/B conversion test, then reads a two-tailed p-value from a standard normal CDF lookup table and flags significance at 0.05. It tests turning a pooled two-proportion z-test into SQL, avoiding integer division, and rounding and capping a value so it matches a lookup key.

Two-Tailed P-Value for a Two-Proportion Z-Test from a Normal-CDF Lookup

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `trials` table has one row per user, with `group_name` (`'control'` or `'treatment'`) and `converted` (1 or 0). The `normal_cdf` table is a standard normal CDF lookup with `z` from 0.0 to 4.0 in steps of 0.1 and `cdf` equal to $\Phi(z)$. Return one row with `z_abs`, `p_value` and `significant`, computed as follows: - Compute the pooled two-proportion z-statistic comparing the conversion rates of the two groups. - `z_abs` is the absolute value of z rounded to 1 decimal place, then clamped into the range 0.0 to 4.0. - `p_value` is ${2 \cdot (1 - \Phi(z_{abs}))}$, with $\Phi$ read from `normal_cdf`, rounded to 4 decimal places. - `significant` is 1 when `p_value < 0.05`, else 0. With $n_c, n_t$ users and $x_c, x_t$ conversions in control and treatment, $\hat p_c = x_c / n_c$, $\hat p_t = x_t / n_t$ and the pooled rate $\hat p = (x_c + x_t) / (n_c + n_t)$: $$ z = \frac{\hat p_c - \hat p_t}{\sqrt{\hat p (1 - \hat p) \left( \frac{1}{n_c} + \frac{1}{n_t} \right)}} $$ ```hint Landing on a key The lookup only has values at steps of 0.1 up to 4.0. Make sure the value you join on always matches one of its rows, and that the reported `z_abs` is the same value you looked up. ``` ### Input Table(s) `trials` | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | `'control'` or `'treatment'` | | converted | TINYINT | 1 if the user converted, else 0 | `trials` has one row per user and no other key. `normal_cdf` | Column | Type | Meaning | |---|---|---| | z | DECIMAL(3,1) | A z value from 0.0 to 4.0 | | cdf | DECIMAL(6,4) | $\Phi(z)$ rounded to 4 decimals | Primary key: `z`. The table has exactly one row for each of the 41 values 0.0, 0.1, ..., 4.0. ### Output Contract Return exactly one row with these columns, in this order: | Column | Meaning | |---|---| | z_abs | `ABS(z)` rounded to 1 decimal place, capped at 4.0 | | p_value | `2 * (1 - cdf)` for the lookup row whose `z` equals `z_abs`, rounded to 4 decimal places | | significant | 1 when `p_value < 0.05`, else 0 | ### Example Example 1. Input `trials`: | group_name | converted | |---|---| | treatment | 1 | | treatment | 0 | | control | 0 | | treatment | 1 | | treatment | 1 | | treatment | 1 | | treatment | 0 | | control | 0 | | control | 1 | | control | 1 | | control | 0 | | control | 1 | | control | 0 | | control | 0 | | treatment | 1 | | treatment | 1 | | control | 0 | | treatment | 1 | Input `normal_cdf`: | z | cdf | |---|---| | 0.0 | 0.5000 | | 0.1 | 0.5398 | | 0.2 | 0.5793 | | 0.3 | 0.6179 | | 0.4 | 0.6554 | | 0.5 | 0.6915 | | 0.6 | 0.7257 | | 0.7 | 0.7580 | | 0.8 | 0.7881 | | 0.9 | 0.8159 | | 1.0 | 0.8413 | | 1.1 | 0.8643 | | 1.2 | 0.8849 | | 1.3 | 0.9032 | | 1.4 | 0.9192 | | 1.5 | 0.9332 | | 1.6 | 0.9452 | | 1.7 | 0.9554 | | 1.8 | 0.9641 | | 1.9 | 0.9713 | | 2.0 | 0.9772 | | 2.1 | 0.9821 | | 2.2 | 0.9861 | | 2.3 | 0.9893 | | 2.4 | 0.9918 | | 2.5 | 0.9938 | | 2.6 | 0.9953 | | 2.7 | 0.9965 | | 2.8 | 0.9974 | | 2.9 | 0.9981 | | 3.0 | 0.9987 | | 3.1 | 0.9990 | | 3.2 | 0.9993 | | 3.3 | 0.9995 | | 3.4 | 0.9997 | | 3.5 | 0.9998 | | 3.6 | 0.9998 | | 3.7 | 0.9999 | | 3.8 | 0.9999 | | 3.9 | 1.0000 | | 4.0 | 1.0000 | Expected output: | z_abs | p_value | significant | |---|---|---| | 1.9 | 0.0574 | 0 | Control converts 3 of 9 users and treatment 7 of 9, so the pooled rate is 10 / 18. The standard error is $\sqrt{(10/18)(8/18)(2/9)} \approx 0.2342$ and z is about -0.4444 / 0.2342 = -1.8974. Its absolute value rounds to 1.9, where the lookup gives 0.9713, so the p-value is 2 times 0.0287, which is 0.0574. That is not below 0.05. Example 2. Input `trials` has 9 rows with `group_name = 'control'` and `converted = 0`, and 9 rows with `group_name = 'treatment'` and `converted = 1`. `normal_cdf` is the same as in Example 1. Expected output: | z_abs | p_value | significant | |---|---|---| | 4.0 | 0.0000 | 1 | The pooled rate is 0.5, the standard error is $\sqrt{0.25 \cdot 2/9} \approx 0.2357$, and the absolute z is about 4.2426. It rounds to 4.2 and is capped at 4.0, where the lookup gives 1.0000, so the p-value is 0. ### Constraints and Clarifications - `group_name` is always `'control'` or `'treatment'`, and `converted` is always 0 or 1. Neither column is NULL. - Each group has at least one user, and at least one user overall converted and at least one did not, so the standard error is positive. - Round the absolute z to 1 decimal with halves rounding up, then cap it at 4.0. `z_abs` reports the capped value. - The sign of z does not matter, because only its absolute value is used. - `significant` uses a strict comparison, so a p-value of exactly 0.0500 gives 0.

Overview: Write a SQL query that computes a pooled two-proportion z-statistic for an A/B conversion test, then reads a two-tailed p-value from a standard normal CDF lookup table and flags significance at 0.05. It tests turning a pooled two-proportion z-test into SQL, avoiding integer division, and rounding and capping a value so it matches a lookup key.

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

The trials table has one row per user, with group_name ('control' or 'treatment') and converted (1 or 0). The normal_cdf table is a standard normal CDF lookup with z from 0.0 to 4.0 in steps of 0.1 and cdf equal to Φ(z)\Phi(z). Return one row with z_abs, p_value and significant, computed as follows:

  • Compute the pooled two-proportion z-statistic comparing the conversion rates of the two groups.
  • z_abs is the absolute value of z rounded to 1 decimal place, then clamped into the range 0.0 to 4.0.
  • p_value is 2⋅(1−Φ(zabs)){2 \cdot (1 - \Phi(z_{abs}))} , with Φ\Phi read from normal_cdf , rounded to 4 decimal places.
  • significant is 1 when p_value < 0.05 , else 0.

With nc,ntn_c, n_t users and xc,xtx_c, x_t conversions in control and treatment, p^c=xc/nc\hat p_c = x_c / n_c, p^t=xt/nt\hat p_t = x_t / n_t and the pooled rate p^=(xc+xt)/(nc+nt)\hat p = (x_c + x_t) / (n_c + n_t):

z=p^c−p^tp^(1−p^)(1nc+1nt)z = \frac{\hat p_c - \hat p_t}{\sqrt{\hat p (1 - \hat p) \left( \frac{1}{n_c} + \frac{1}{n_t} \right)}}

Input Table(s)

trials

ColumnTypeMeaning
group_nameVARCHAR(20)'control' or 'treatment'
convertedTINYINT1 if the user converted, else 0

trials has one row per user and no other key.

normal_cdf

ColumnTypeMeaning
zDECIMAL(3,1)A z value from 0.0 to 4.0
cdfDECIMAL(6,4)Φ(z)\Phi(z) rounded to 4 decimals

Primary key: z. The table has exactly one row for each of the 41 values 0.0, 0.1, ..., 4.0.

Output Contract

Return exactly one row with these columns, in this order:

ColumnMeaning
z_absABS(z) rounded to 1 decimal place, capped at 4.0
p_value2 * (1 - cdf) for the lookup row whose z equals z_abs, rounded to 4 decimal places
significant1 when p_value < 0.05, else 0

Example

Example 1. Input trials:

group_nameconverted
treatment1
treatment0
control0
treatment1
treatment1
treatment1
treatment0
control0
control1
control1
control0
control1
control0
control0
treatment1
treatment1
control0
treatment1

Input normal_cdf:

zcdf
0.00.5000
0.10.5398
0.20.5793
0.30.6179
0.40.6554
0.50.6915
0.60.7257
0.70.7580
0.80.7881
0.90.8159
1.00.8413
1.10.8643
1.20.8849
1.30.9032
1.40.9192
1.50.9332
1.60.9452
1.70.9554
1.80.9641
1.90.9713
2.00.9772
2.10.9821
2.20.9861
2.30.9893
2.40.9918
2.50.9938
2.60.9953
2.70.9965
2.80.9974
2.90.9981
3.00.9987
3.10.9990
3.20.9993
3.30.9995
3.40.9997
3.50.9998
3.60.9998
3.70.9999
3.80.9999
3.91.0000
4.01.0000

Expected output:

z_absp_valuesignificant
1.90.05740

Control converts 3 of 9 users and treatment 7 of 9, so the pooled rate is 10 / 18. The standard error is (10/18)(8/18)(2/9)≈0.2342\sqrt{(10/18)(8/18)(2/9)} \approx 0.2342 and z is about -0.4444 / 0.2342 = -1.8974. Its absolute value rounds to 1.9, where the lookup gives 0.9713, so the p-value is 2 times 0.0287, which is 0.0574. That is not below 0.05.

Example 2. Input trials has 9 rows with group_name = 'control' and converted = 0, and 9 rows with group_name = 'treatment' and converted = 1. normal_cdf is the same as in Example 1.

Expected output:

z_absp_valuesignificant
4.00.00001

The pooled rate is 0.5, the standard error is 0.25⋅2/9≈0.2357\sqrt{0.25 \cdot 2/9} \approx 0.2357, and the absolute z is about 4.2426. It rounds to 4.2 and is capped at 4.0, where the lookup gives 1.0000, so the p-value is 0.

Constraints and Clarifications

  • group_name is always 'control' or 'treatment' , and converted is always 0 or 1. Neither column is NULL.
  • Each group has at least one user, and at least one user overall converted and at least one did not, so the standard error is positive.
  • Round the absolute z to 1 decimal with halves rounding up, then cap it at 4.0. z_abs reports the capped value.
  • The sign of z does not matter, because only its absolute value is used.
  • significant uses a strict comparison, so a p-value of exactly 0.0500 gives 0.