Compute the standard error of the mean and the sample size in SQL
Quick Overview
Write a SQL query that returns the standard error of the mean for a column of measurements, rounded to four decimals, together with the sample size. It tests choosing the sample standard deviation, numeric types and rounding in PostgreSQL, and the single-row edge case.
Compute the standard error of the mean and the sample size in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `measurements` table has one numeric column `value`, one row per observation. Compute the standard error of the mean,
$$\text{SEM} = \frac{s}{\sqrt{n}}$$
where $s$ is the sample standard deviation and $n$ is the row count. Return one row with `standard_error` rounded to 4 decimals and the sample size as `n`.
```hint Which standard deviation
PostgreSQL has more than one standard deviation aggregate. Check which denominator each one uses before picking.
```
### Input Table(s)
Table `measurements`
| Column | Type | Meaning |
|---|---|---|
| value | DECIMAL(10,4) | One observation |
Each row is one observation. The table has no primary key, and repeated values are separate observations.
### Output Contract
- Exactly one row with the columns `standard_error` and `n`, in that order.
- `standard_error` is $s / \sqrt{n}$ rounded to 4 decimal places, with $s$ computed using the $n - 1$ denominator.
- `n` is the integer row count.
### Example
Input `measurements`:
| value |
|---|
| 68.2 |
| 65.5 |
| 66.7 |
| 73.1 |
| 75.8 |
| 49.9 |
| 42.1 |
| 75.5 |
Expected output:
| standard_error | n |
|---|---|
| 4.3446 | 8 |
The mean is 64.6 and the squared deviations sum to 1057.02, so ${s^2 = 1057.02 / 7 \approx 151.0029}$ and $s \approx 12.2883$. Dividing by $\sqrt{8} \approx 2.8284$ gives about 4.34458, which rounds to 4.3446.
### Constraints and Clarifications
- `value` is never NULL.
- With a single row the sample standard deviation is undefined. Return `standard_error` as NULL and `n` as 1 in that case.
- Duplicate values all count toward $n$ and toward $s$.
Overview: Write a SQL query that returns the standard error of the mean for a column of measurements, rounded to four decimals, together with the sample size. It tests choosing the sample standard deviation, numeric types and rounding in PostgreSQL, and the single-row edge case.
The measurements table has one numeric column value, one row per observation. Compute the standard error of the mean,
SEM=ns
where s is the sample standard deviation and n is the row count. Return one row with standard_error rounded to 4 decimals and the sample size as n.
Input Table(s)
Table measurements
Column
Type
Meaning
value
DECIMAL(10,4)
One observation
Each row is one observation. The table has no primary key, and repeated values are separate observations.
Output Contract
Exactly one row with the columns
standard_error
and
n
, in that order.
standard_error
is
s/n
rounded to 4 decimal places, with
s
computed using the
n−1
denominator.
n
is the integer row count.
Example
Input measurements:
value
68.2
65.5
66.7
73.1
75.8
49.9
42.1
75.5
Expected output:
standard_error
n
4.3446
8
The mean is 64.6 and the squared deviations sum to 1057.02, so s2=1057.02/7≈151.0029 and s≈12.2883. Dividing by 8≈2.8284 gives about 4.34458, which rounds to 4.3446.
Constraints and Clarifications
value
is never NULL.
With a single row the sample standard deviation is undefined. Return
standard_error
as NULL and
n
as 1 in that case.
Duplicate values all count toward
n
and toward
s
.