Empirical Percentile of a Threshold Score by Cohort Using CUME_DIST

Quick Overview

Write a SQL query that finds, for each cohort, the fraction of scores at or below a threshold stored in a one-row parameters table. It tests how CUME_DIST works with partitions and ties, cross joining a parameter row, and covering cohorts where no score qualifies.

Empirical Percentile of a Threshold Score by Cohort Using CUME_DIST

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `scores` table holds one `score` per row for cohort `'A'` or `'B'`, and the single-row `params` table supplies one `threshold`. For each cohort, use `CUME_DIST()` over that cohort's scores to find the fraction of its scores that are less than or equal to `threshold`. Return one row per cohort with `cohort` and `threshold_percentile` rounded to 4 decimals. ```hint Which row answers it Work out which row's `CUME_DIST()` value equals the fraction you want, and decide what a cohort with no score at or below the threshold should return. ``` ### Input Table(s) `scores` | Column | Type | Meaning | |---|---|---| | cohort | VARCHAR(5) | Cohort label, `'A'` or `'B'` | | score | DECIMAL(8,1) | One observed score | `scores` has no primary key. Each row is one observation, and two rows may have the same cohort and score. `params` | Column | Type | Meaning | |---|---|---| | threshold | DECIMAL(8,1) | The value whose percentile is wanted | `params` has exactly one row. ### Output Contract Return these columns, in this order: | Column | Meaning | |---|---| | cohort | The cohort label | | threshold_percentile | Fraction of the cohort's scores that are less than or equal to `threshold`, rounded to 4 decimal places | Return one row for each cohort that has at least one row in `scores`, ordered by `cohort` ascending. ### Example Example 1. Input `scores`: | cohort | score | |---|---| | B | 72.0 | | B | 65.3 | | A | 46.0 | | A | 20.6 | | A | 37.2 | | B | 64.6 | | B | 52.3 | | A | 64.4 | | B | 72.6 | | A | 34.1 | | A | 56.4 | | B | 39.3 | | A | 58.9 | Input `params`: | threshold | |---| | 50.0 | Expected output: | cohort | threshold_percentile | |---|---| | A | 0.5714 | | B | 0.1667 | Cohort A has 7 scores, and 4 of them (20.6, 34.1, 37.2 and 46.0) are at or below 50, so the result is 4 / 7 = 0.5714. Cohort B has 6 scores and only 39.3 qualifies, so the result is 1 / 6 = 0.1667. Example 2. Input `scores`: | cohort | score | |---|---| | A | 10.0 | | A | 10.0 | | A | 12.5 | | A | 8.0 | | B | 15.0 | | B | 11.0 | | B | 20.0 | Input `params`: | threshold | |---| | 10.0 | Expected output: | cohort | threshold_percentile | |---|---| | A | 0.7500 | | B | 0.0000 | In cohort A, both rows equal to the threshold count, along with 8.0, giving 3 / 4. No score in cohort B is at or below 10, so its result is 0. ### Constraints and Clarifications - `cohort` and `score` are never NULL, and `threshold` is not NULL. - A score equal to `threshold` counts as at or below it. - Rows with equal scores are separate observations and each one counts. - A cohort whose scores are all above `threshold` returns 0. - A cohort with no rows in `scores` does not appear in the output.

Overview: Write a SQL query that finds, for each cohort, the fraction of scores at or below a threshold stored in a one-row parameters table. It tests how CUME_DIST works with partitions and ties, cross joining a parameter row, and covering cohorts where no score qualifies.

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

The scores table holds one score per row for cohort 'A' or 'B', and the single-row params table supplies one threshold. For each cohort, use CUME_DIST() over that cohort's scores to find the fraction of its scores that are less than or equal to threshold. Return one row per cohort with cohort and threshold_percentile rounded to 4 decimals.

Input Table(s)

scores

ColumnTypeMeaning
cohortVARCHAR(5)Cohort label, 'A' or 'B'
scoreDECIMAL(8,1)One observed score

scores has no primary key. Each row is one observation, and two rows may have the same cohort and score.

params

ColumnTypeMeaning
thresholdDECIMAL(8,1)The value whose percentile is wanted

params has exactly one row.

Output Contract

Return these columns, in this order:

ColumnMeaning
cohortThe cohort label
threshold_percentileFraction of the cohort's scores that are less than or equal to threshold, rounded to 4 decimal places

Return one row for each cohort that has at least one row in scores, ordered by cohort ascending.

Example

Example 1. Input scores:

cohortscore
B72.0
B65.3
A46.0
A20.6
A37.2
B64.6
B52.3
A64.4
B72.6
A34.1
A56.4
B39.3
A58.9

Input params:

threshold
50.0

Expected output:

cohortthreshold_percentile
A0.5714
B0.1667

Cohort A has 7 scores, and 4 of them (20.6, 34.1, 37.2 and 46.0) are at or below 50, so the result is 4 / 7 = 0.5714. Cohort B has 6 scores and only 39.3 qualifies, so the result is 1 / 6 = 0.1667.

Example 2. Input scores:

cohortscore
A10.0
A10.0
A12.5
A8.0
B15.0
B11.0
B20.0

Input params:

threshold
10.0

Expected output:

cohortthreshold_percentile
A0.7500
B0.0000

In cohort A, both rows equal to the threshold count, along with 8.0, giving 3 / 4. No score in cohort B is at or below 10, so its result is 0.

Constraints and Clarifications

  • cohort and score are never NULL, and threshold is not NULL.
  • A score equal to threshold counts as at or below it.
  • Rows with equal scores are separate observations and each one counts.
  • A cohort whose scores are all above threshold returns 0.
  • A cohort with no rows in scores does not appear in the output.