Welch's t-Test Statistic

Quick Overview

Write a PostgreSQL query that computes Welch's t-statistic comparing control and treatment means without pooling the variances. It tests conditional aggregation, sample variance and the difference between the Welch and pooled two-sample t-tests.

Welch's t-Test Statistic

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `measurements` holds one observation per row in `value`, labeled by `group_name`: `'control'` is group 1 and `'treatment'` is group 2. Compute Welch's t-statistic $$ t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}} $$ where $s_1^2$ and $s_2^2$ are the sample variances and $n_1$, $n_2$ are the group sizes. Return one row with `t_statistic` rounded to 4 decimals. ```hint One scan is enough Every per-group quantity in the formula can come from a single pass over the table if each aggregate only sees the values of one group. ``` ### Input Table(s) Table `measurements`: | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | `'control'` (group 1) or `'treatment'` (group 2) | | value | DECIMAL(10,4) | The measured outcome for one observation | The table has no primary key. Each row is one observation, and identical rows are separate observations. ### Output Contract Exactly one row with one column: | Column | Type | Meaning | |---|---|---| | t_statistic | numeric | Welch's t-statistic, control minus treatment, rounded to 4 decimal places | The result always has one row, so no ordering is needed. ### Example ```text Input: measurements +------------+-------+ | group_name | value | +------------+-------+ | treatment | 10.4 | | control | 4.4 | | treatment | 9.8 | | treatment | 8.2 | | control | 4.9 | | control | 6.5 | | control | 4 | | treatment | 6.6 | | treatment | 10 | +------------+-------+ Output: +-------------+ | t_statistic | +-------------+ | -4.5257 | +-------------+ ``` Control has 4 rows with mean 4.95 and sample variance 1.2033. Treatment has 5 rows with mean 9.0 and sample variance 2.5. The standard error is $\sqrt{1.2033/4 + 2.5/5} \approx 0.8949$, and $-4.05 / 0.8949 \approx -4.5257$. ### Constraints and Clarifications - `group_name` only takes the values `'control'` and `'treatment'`. - `value` is never NULL. - Each group has at least two rows, and the standard error is greater than zero. - Sample variance uses the $n - 1$ denominator, and the variances are not pooled. - Duplicate rows all count as observations. - Round only the final statistic.

Overview: Write a PostgreSQL query that computes Welch's t-statistic comparing control and treatment means without pooling the variances. It tests conditional aggregation, sample variance and the difference between the Welch and pooled two-sample t-tests.

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

Table measurements holds one observation per row in value, labeled by group_name: 'control' is group 1 and 'treatment' is group 2. Compute Welch's t-statistic

t=xˉ1−xˉ2s12/n1+s22/n2t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}}

where s12s_1^2 and s22s_2^2 are the sample variances and n1n_1, n2n_2 are the group sizes. Return one row with t_statistic rounded to 4 decimals.

Input Table(s)

Table measurements:

ColumnTypeMeaning
group_nameVARCHAR(20)'control' (group 1) or 'treatment' (group 2)
valueDECIMAL(10,4)The measured outcome for one observation

The table has no primary key. Each row is one observation, and identical rows are separate observations.

Output Contract

Exactly one row with one column:

ColumnTypeMeaning
t_statisticnumericWelch's t-statistic, control minus treatment, rounded to 4 decimal places

The result always has one row, so no ordering is needed.

Example

Input: measurements
+------------+-------+
| group_name | value |
+------------+-------+
| treatment  | 10.4  |
| control    | 4.4   |
| treatment  | 9.8   |
| treatment  | 8.2   |
| control    | 4.9   |
| control    | 6.5   |
| control    | 4     |
| treatment  | 6.6   |
| treatment  | 10    |
+------------+-------+

Output:
+-------------+
| t_statistic |
+-------------+
| -4.5257     |
+-------------+

Control has 4 rows with mean 4.95 and sample variance 1.2033. Treatment has 5 rows with mean 9.0 and sample variance 2.5. The standard error is 1.2033/4+2.5/5≈0.8949\sqrt{1.2033/4 + 2.5/5} \approx 0.8949, and −4.05/0.8949≈−4.5257-4.05 / 0.8949 \approx -4.5257.

Constraints and Clarifications

  • group_name only takes the values 'control' and 'treatment' .
  • value is never NULL.
  • Each group has at least two rows, and the standard error is greater than zero.
  • Sample variance uses the n−1n - 1 denominator, and the variances are not pooled.
  • Duplicate rows all count as observations.
  • Round only the final statistic.