One-Sample t-Test Statistic

Quick Overview

Compute the one-sample t-statistic in SQL for whether a population mean differs from 100, along with its degrees of freedom. It tests sample standard deviation aggregates, building a standard error from the row count, and numeric rounding in PostgreSQL.

One-Sample t-Test Statistic

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Using table `samples`, compute the one-sample t-statistic testing whether the population mean differs from 100: $$t = \frac{\text{mean} - 100}{s / \sqrt{n}}$$ where $s$ is the sample standard deviation and $n$ is the row count. Return one row with `t_statistic` (rounded to 4 decimals) and `df` (n - 1). ```hint Check the divisor and the types The sample standard deviation divides by n - 1, so make sure the aggregate you pick does that. Also check what type the expression has before you call ROUND in PostgreSQL. ``` ### Input Table(s) Table `samples` | Column | Type | Meaning | |---|---|---| | `measurement` | NUMERIC(10,4) | one observed value | Keys: none. Each row is one observation. ### Output Contract Exactly one row with these columns, in this order: | Column | Type | Meaning | |---|---|---| | `t_statistic` | NUMERIC | the t-statistic rounded to 4 decimal places | | `df` | INTEGER | the row count minus 1 | The result is a single row, so no ordering is needed. ### Example Input `samples`: | measurement | |---| | 102.7 | | 105.1 | | 95.1 | | 98 | | 105.8 | | 102.7 | | 103.6 | | 97.6 | Expected output: | t_statistic | df | |---|---| | 0.9589 | 7 | There are 8 measurements with a mean of 101.325, so the mean sits 1.325 above 100. The sample standard deviation is about 3.9081, which makes the standard error $s / \sqrt{8} \approx 1.3817$ and $t \approx 1.325 / 1.3817 \approx 0.9589$. The degrees of freedom are 8 - 1 = 7. ### Constraints and Clarifications - `samples` has at least 2 rows and the measurements are not all equal, so $s > 0$. - `measurement` is never NULL. - The hypothesized mean is 100. - Round `t_statistic` with PostgreSQL `ROUND(numeric, 4)`, applied to the final statistic only.

Overview: Compute the one-sample t-statistic in SQL for whether a population mean differs from 100, along with its degrees of freedom. It tests sample standard deviation aggregates, building a standard error from the row count, and numeric rounding in PostgreSQL.

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

Using table samples, compute the one-sample t-statistic testing whether the population mean differs from 100:

t=mean−100s/nt = \frac{\text{mean} - 100}{s / \sqrt{n}}

where ss is the sample standard deviation and nn is the row count. Return one row with t_statistic (rounded to 4 decimals) and df (n - 1).

Input Table(s)

Table samples

ColumnTypeMeaning
measurementNUMERIC(10,4)one observed value

Keys: none. Each row is one observation.

Output Contract

Exactly one row with these columns, in this order:

ColumnTypeMeaning
t_statisticNUMERICthe t-statistic rounded to 4 decimal places
dfINTEGERthe row count minus 1

The result is a single row, so no ordering is needed.

Example

Input samples:

measurement
102.7
105.1
95.1
98
105.8
102.7
103.6
97.6

Expected output:

t_statisticdf
0.95897

There are 8 measurements with a mean of 101.325, so the mean sits 1.325 above 100. The sample standard deviation is about 3.9081, which makes the standard error s/8≈1.3817s / \sqrt{8} \approx 1.3817 and t≈1.325/1.3817≈0.9589t \approx 1.325 / 1.3817 \approx 0.9589. The degrees of freedom are 8 - 1 = 7.

Constraints and Clarifications

  • samples has at least 2 rows and the measurements are not all equal, so s>0s > 0 .
  • measurement is never NULL.
  • The hypothesized mean is 100.
  • Round t_statistic with PostgreSQL ROUND(numeric, 4) , applied to the final statistic only.