Confidence Interval for a Mean with a Supplied t Value

Quick Overview

Write a SQL query that builds a 95% confidence interval for a mean by aggregating a sample and looking up the t critical value for n - 1 degrees of freedom. It tests aggregation, joining a lookup table on a computed key, and rounding the final bounds without losing precision.

Confidence Interval for a Mean with a Supplied t Value

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `samples` has one numeric `value` per observation. Table `t_critical` stores the critical `t_value` for a 95% interval at each degrees of freedom `df`. With n observations, sample mean m and sample standard deviation s, compute the 95% interval $m \pm t \cdot s / \sqrt{n}$, where t is the `t_value` for `df` = n - 1. Return one row with `ci_lower` and `ci_upper`, each rounded to 4 decimals. ```hint Order of work The `df` you need to look up depends on n, and n is only known once `samples` has been aggregated. ``` ### Input Table(s) Table `samples`: | Column | Type | Meaning | |---|---|---| | `value` | DECIMAL(10,1) | One observation | The table has no primary key. Repeated values are separate observations. Table `t_critical`: | Column | Type | Meaning | |---|---|---| | `df` | INT | Degrees of freedom | | `t_value` | DECIMAL(8,4) | Critical t value for a 95% interval at this `df` | `df` is unique: the table has at most one row per degrees of freedom. ### Output Contract - Exactly one row with the columns `ci_lower`, `ci_upper`, in that order. - `ci_lower` = m - t_value * s / sqrt(n) and `ci_upper` = m + t_value * s / sqrt(n), using the `t_value` whose `df` is n - 1. - s is the sample standard deviation (divisor n - 1). - Both bounds are rounded to 4 decimal places. Round only the final bounds. ### Example Input `samples`: | value | |---| | 16.1 | | 15 | | 11.6 | | 12.9 | | 12 | | 10.2 | | 17.8 | | 10.9 | | 12 | | 17.5 | | 16 | | 17 | | 17.8 | | 11 | | 11 | Input `t_critical`: | df | t_value | |---|---| | 14 | 2.1448 | | 24 | 2.0639 | Expected output: | ci_lower | ci_upper | |---|---| | 12.3227 | 15.5173 | There are 15 observations, so the lookup uses `df` = 14 and t = 2.1448. The mean is 208.8 / 15 = 13.92 and the standard error is about 0.7447083, so the margin is about 1.5972504. The unrounded bounds are about 12.3227496 and 15.5172504, which round to 12.3227 and 15.5173. ### Constraints and Clarifications - `value` is never NULL, and `samples` has at least two rows. - `t_critical` always contains a row for `df` = n - 1. Rows for other `df` values are ignored. - Duplicate values in `samples` are separate observations and all of them count. - The output is a single row, so there is no ordering or tie rule.

Overview: Write a SQL query that builds a 95% confidence interval for a mean by aggregating a sample and looking up the t critical value for n - 1 degrees of freedom. It tests aggregation, joining a lookup table on a computed key, and rounding the final bounds without losing precision.

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

Table samples has one numeric value per observation. Table t_critical stores the critical t_value for a 95% interval at each degrees of freedom df. With n observations, sample mean m and sample standard deviation s, compute the 95% interval m±t⋅s/nm \pm t \cdot s / \sqrt{n}, where t is the t_value for df = n - 1. Return one row with ci_lower and ci_upper, each rounded to 4 decimals.

Input Table(s)

Table samples:

ColumnTypeMeaning
valueDECIMAL(10,1)One observation

The table has no primary key. Repeated values are separate observations.

Table t_critical:

ColumnTypeMeaning
dfINTDegrees of freedom
t_valueDECIMAL(8,4)Critical t value for a 95% interval at this df

df is unique: the table has at most one row per degrees of freedom.

Output Contract

  • Exactly one row with the columns ci_lower , ci_upper , in that order.
  • ci_lower = m - t_value * s / sqrt(n) and ci_upper = m + t_value * s / sqrt(n), using the t_value whose df is n - 1.
  • s is the sample standard deviation (divisor n - 1).
  • Both bounds are rounded to 4 decimal places. Round only the final bounds.

Example

Input samples:

value
16.1
15
11.6
12.9
12
10.2
17.8
10.9
12
17.5
16
17
17.8
11
11

Input t_critical:

dft_value
142.1448
242.0639

Expected output:

ci_lowerci_upper
12.322715.5173

There are 15 observations, so the lookup uses df = 14 and t = 2.1448. The mean is 208.8 / 15 = 13.92 and the standard error is about 0.7447083, so the margin is about 1.5972504. The unrounded bounds are about 12.3227496 and 15.5172504, which round to 12.3227 and 15.5173.

Constraints and Clarifications

  • value is never NULL, and samples has at least two rows.
  • t_critical always contains a row for df = n - 1. Rows for other df values are ignored.
  • Duplicate values in samples are separate observations and all of them count.
  • The output is a single row, so there is no ordering or tie rule.