Difference-in-Means Z-Test p-Value from a Normal CDF Lookup Table in SQL

Quick Overview

Write a SQL query that computes a two-sample Z-statistic for control versus treatment means and approximates its two-tailed p-value from a standard normal CDF lookup table. It tests conditional aggregation, building an exact join key by rounding and capping, and p-value arithmetic in PostgreSQL.

Difference-in-Means Z-Test p-Value from a Normal CDF Lookup Table in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `measurements` holds one continuous measurement per row, labeled `'control'` or `'treatment'`. Table `normal_cdf` maps `z` values from 0.0 to 3.5, in steps of 0.1, to the standard normal CDF `phi`. Compute the two-sample Z-statistic for the difference in means, with `'control'` as group 1, and its approximate two-tailed p-value from the lookup table. Return one row with `z_statistic` and `p_value`. The rules are: - The statistic is $z = \dfrac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}}$, where $s_i^2$ is the sample variance of group $i$. - The lookup key is $|z|$ rounded to 1 decimal and capped at 3.5. It is joined to `normal_cdf.z`. - The p-value is ${2(1 - \phi)}$, using the `phi` of the matched row. - `z_statistic` and `p_value` are each rounded to 4 decimals. ```hint Building the key Compute the statistic first and keep it unrounded. The lookup key comes from its absolute value, and it has to equal a `normal_cdf.z` value exactly for the join to match. ``` ### Input Table(s) `measurements` | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | `'control'` or `'treatment'` | | value | DECIMAL(10,4) | The continuous measurement | There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations. `normal_cdf` | Column | Type | Meaning | |---|---|---| | z | DECIMAL(3,1) | A non-negative z value: 0.0, 0.1, ..., 3.5 | | phi | DECIMAL(6,4) | Standard normal CDF at `z` | `z` is the primary key. The table has 36 rows, one for each step from 0.0 to 3.5. ### Output Contract Exactly one row with these columns, in this order: | Column | Meaning | |---|---| | z_statistic | The Z-statistic, control minus treatment, rounded to 4 decimals | | p_value | ${2(1 - \phi)}$ for the matched lookup row, rounded to 4 decimals | Round `z_statistic` only for output. The lookup key is built from the unrounded statistic. The result is a single row, so no ordering applies. ### Example `measurements` | group_name | value | |---|---| | control | 48.6 | | treatment | 49 | | control | 47.3 | | treatment | 51.6 | | treatment | 53.2 | | control | 50.7 | | control | 49.4 | | control | 47.5 | | control | 50.2 | | control | 52.5 | | control | 52 | | treatment | 50.3 | | treatment | 49.7 | | treatment | 51.6 | | treatment | 48.7 | | control | 48.9 | | treatment | 49.8 | | control | 52.7 | | treatment | 50.3 | | treatment | 51.1 | | treatment | 48.3 | `normal_cdf` | z | phi | |---|---| | 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 | Expected result: | z_statistic | p_value | |---|---| | -0.4545 | 0.6170 | Control has 10 values with mean 49.98 and sample variance 3.904. Treatment has 11 values with mean 50.3273 and sample variance 2.1282. The standard error is about 0.7641, so $z \approx -0.3473 / 0.7641 \approx -0.4545$. Its absolute value rounds to 0.5, where `phi` is 0.6915, and the p-value is ${2(1 - 0.6915) = 0.6170}$. ### Constraints and Clarifications - Each group has at least 2 non-NULL values, and the two sample variances are not both zero, so the standard error is positive. - A row whose `value` is NULL is ignored by every per-group statistic: the count, the mean and the variance. - Duplicate rows are separate measurements. Do not deduplicate them. - `group_name` holds only the two lowercase labels `'control'` and `'treatment'`. - The variances are sample variances, with $n - 1$ in the denominator, and they are not pooled. - Rounding to 1 decimal for the key rounds halves away from zero, as `ROUND` does on PostgreSQL `numeric`. - Any $|z|$ above 3.5 uses the 3.5 row.

Overview: Write a SQL query that computes a two-sample Z-statistic for control versus treatment means and approximates its two-tailed p-value from a standard normal CDF lookup table. It tests conditional aggregation, building an exact join key by rounding and capping, and p-value arithmetic in PostgreSQL.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Table measurements holds one continuous measurement per row, labeled 'control' or 'treatment'. Table normal_cdf maps z values from 0.0 to 3.5, in steps of 0.1, to the standard normal CDF phi. Compute the two-sample Z-statistic for the difference in means, with 'control' as group 1, and its approximate two-tailed p-value from the lookup table. Return one row with z_statistic and p_value.

The rules are:

  • The statistic is z=xˉ1−xˉ2s12/n1+s22/n2z = \dfrac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}} , where si2s_i^2 is the sample variance of group ii .
  • The lookup key is ∣z∣|z| rounded to 1 decimal and capped at 3.5. It is joined to normal_cdf.z .
  • The p-value is 2(1−ϕ){2(1 - \phi)} , using the phi of the matched row.
  • z_statistic and p_value are each rounded to 4 decimals.

Input Table(s)

measurements

ColumnTypeMeaning
group_nameVARCHAR(20)'control' or 'treatment'
valueDECIMAL(10,4)The continuous measurement

There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations.

normal_cdf

ColumnTypeMeaning
zDECIMAL(3,1)A non-negative z value: 0.0, 0.1, ..., 3.5
phiDECIMAL(6,4)Standard normal CDF at z

z is the primary key. The table has 36 rows, one for each step from 0.0 to 3.5.

Output Contract

Exactly one row with these columns, in this order:

ColumnMeaning
z_statisticThe Z-statistic, control minus treatment, rounded to 4 decimals
p_value2(1−ϕ){2(1 - \phi)} for the matched lookup row, rounded to 4 decimals

Round z_statistic only for output. The lookup key is built from the unrounded statistic. The result is a single row, so no ordering applies.

Example

measurements

group_namevalue
control48.6
treatment49
control47.3
treatment51.6
treatment53.2
control50.7
control49.4
control47.5
control50.2
control52.5
control52
treatment50.3
treatment49.7
treatment51.6
treatment48.7
control48.9
treatment49.8
control52.7
treatment50.3
treatment51.1
treatment48.3

normal_cdf

zphi
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

Expected result:

z_statisticp_value
-0.45450.6170

Control has 10 values with mean 49.98 and sample variance 3.904. Treatment has 11 values with mean 50.3273 and sample variance 2.1282. The standard error is about 0.7641, so z≈−0.3473/0.7641≈−0.4545z \approx -0.3473 / 0.7641 \approx -0.4545. Its absolute value rounds to 0.5, where phi is 0.6915, and the p-value is 2(1−0.6915)=0.6170{2(1 - 0.6915) = 0.6170}.

Constraints and Clarifications

  • Each group has at least 2 non-NULL values, and the two sample variances are not both zero, so the standard error is positive.
  • A row whose value is NULL is ignored by every per-group statistic: the count, the mean and the variance.
  • Duplicate rows are separate measurements. Do not deduplicate them.
  • group_name holds only the two lowercase labels 'control' and 'treatment' .
  • The variances are sample variances, with n−1n - 1 in the denominator, and they are not pooled.
  • Rounding to 1 decimal for the key rounds halves away from zero, as ROUND does on PostgreSQL numeric .
  • Any ∣z∣|z| above 3.5 uses the 3.5 row.