Coefficient of Variation per Segment

Quick Overview

Write a SQL query that computes the coefficient of variation of a numeric column for each segment, defined as the sample standard deviation divided by the mean. It tests grouped aggregation, picking the right standard deviation function, and rounding results in PostgreSQL.

Coefficient of Variation per Segment

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `metrics` holds positive numeric measurements in `value`, each tagged with a `segment` of `'north'`, `'south'` or `'east'`. Compute each segment's coefficient of variation, `STDDEV_SAMP(value) / AVG(value)`, which is the sample standard deviation divided by the mean. Return one row per `segment` with `segment` and `cv` rounded to 4 decimals. ```hint Which standard deviation SQL offers both a sample and a population standard deviation. Check which denominator the definition uses before you pick a function. ``` ### Input Table(s) Table `metrics`: | Column | Type | Meaning | |---|---|---| | `segment` | VARCHAR(10) | `'north'`, `'south'` or `'east'` | | `value` | DECIMAL(10,4) | A positive measurement | The table has no primary key. Each row is one observation, and the same segment and value can appear in more than one row. ### Output Contract - Columns, in this order: `segment`, `cv`. - One row for each segment that appears in `metrics`. - `cv` is `STDDEV_SAMP(value) / AVG(value)` over that segment's rows, rounded to 4 decimal places. - Rows sorted by `segment` ascending. ### Example Input `metrics`: | segment | value | |---|---| | east | 41.6 | | east | 32.1 | | east | 35.4 | | north | 26.2 | | north | 28.1 | | north | 22.3 | | south | 56.4 | | south | 51.1 | | east | 60.3 | | north | 28.1 | | south | 42.2 | | south | 61.6 | | south | 47.7 | Expected output: | segment | cv | |---|---| | east | 0.2975 | | north | 0.1045 | | south | 0.1453 | East has four values with mean 42.35 and sample standard deviation about 12.598, so its coefficient of variation is about 0.29747, which rounds to 0.2975. ### Constraints and Clarifications - `value` is never NULL and is always greater than 0, so no segment has a mean of zero. - Duplicate rows are separate observations and all of them count. North has 28.1 twice in the example. - A segment with only one row has no sample standard deviation, so its `cv` is NULL. - A segment with no rows does not appear in the output.

Overview: Write a SQL query that computes the coefficient of variation of a numeric column for each segment, defined as the sample standard deviation divided by the mean. It tests grouped aggregation, picking the right standard deviation function, and rounding results in PostgreSQL.

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

Table metrics holds positive numeric measurements in value, each tagged with a segment of 'north', 'south' or 'east'. Compute each segment's coefficient of variation, STDDEV_SAMP(value) / AVG(value), which is the sample standard deviation divided by the mean. Return one row per segment with segment and cv rounded to 4 decimals.

Input Table(s)

Table metrics:

ColumnTypeMeaning
segmentVARCHAR(10)'north', 'south' or 'east'
valueDECIMAL(10,4)A positive measurement

The table has no primary key. Each row is one observation, and the same segment and value can appear in more than one row.

Output Contract

  • Columns, in this order: segment , cv .
  • One row for each segment that appears in metrics .
  • cv is STDDEV_SAMP(value) / AVG(value) over that segment's rows, rounded to 4 decimal places.
  • Rows sorted by segment ascending.

Example

Input metrics:

segmentvalue
east41.6
east32.1
east35.4
north26.2
north28.1
north22.3
south56.4
south51.1
east60.3
north28.1
south42.2
south61.6
south47.7

Expected output:

segmentcv
east0.2975
north0.1045
south0.1453

East has four values with mean 42.35 and sample standard deviation about 12.598, so its coefficient of variation is about 0.29747, which rounds to 0.2975.

Constraints and Clarifications

  • value is never NULL and is always greater than 0, so no segment has a mean of zero.
  • Duplicate rows are separate observations and all of them count. North has 28.1 twice in the example.
  • A segment with only one row has no sample standard deviation, so its cv is NULL.
  • A segment with no rows does not appear in the output.