Ordered-set aggregates in SQL: PERCENTILE_CONT vs PERCENTILE_DISC with WITHIN GROUP

Quick Overview

Asks how SQL ordered-set aggregates such as PERCENTILE_CONT and PERCENTILE_DISC work with WITHIN GROUP, and how continuous and discrete percentiles differ. Tests knowledge of percentile definitions, interpolation, NULL handling and choosing the right function for the data type.

Ordered-set aggregates in SQL: PERCENTILE_CONT vs PERCENTILE_DISC with WITHIN GROUP

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain ordered-set aggregates such as `PERCENTILE_CONT` and `PERCENTILE_DISC` with `WITHIN GROUP`. How do continuous and discrete percentiles differ? ```hint Try a tiny group Sort a group of four values and work out by hand what each function should return for the 0.5 fraction. An even row count is where the two part ways. ``` ```hint Beyond the number Consider which input types each function can accept and whether its result must be a value that appears in the data. ``` ### What a Strong Answer Covers - What makes an aggregate "ordered-set" and the role of `WITHIN GROUP (ORDER BY ...)` - How `PERCENTILE_CONT` computes its result from the neighboring rows - How `PERCENTILE_DISC` picks its result and what kind of value it can return - A small example where the two functions return different values - When to choose each, including data types and NULL handling ### Follow-up Questions - How would you compute the 25th, 50th and 75th percentile salary per department in one query? - How do you get each employee's percentile position within the department, and how does that relate to `PERCENTILE_DISC`? - How would you compute a median in an engine that has no percentile aggregate? - What do these functions return for an empty group or a group where every value is NULL?

Overview: Asks how SQL ordered-set aggregates such as PERCENTILE_CONT and PERCENTILE_DISC work with WITHIN GROUP, and how continuous and discrete percentiles differ. Tests knowledge of percentile definitions, interpolation, NULL handling and choosing the right function for the data type.

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

Explain ordered-set aggregates such as PERCENTILE_CONT and PERCENTILE_DISC with WITHIN GROUP. How do continuous and discrete percentiles differ?

What a Strong Answer Covers Guidance

  • What makes an aggregate "ordered-set" and the role of WITHIN GROUP (ORDER BY ...)
  • How PERCENTILE_CONT computes its result from the neighboring rows
  • How PERCENTILE_DISC picks its result and what kind of value it can return
  • A small example where the two functions return different values
  • When to choose each, including data types and NULL handling

Follow-up Questions Guidance

  • How would you compute the 25th, 50th and 75th percentile salary per department in one query?
  • How do you get each employee's percentile position within the department, and how does that relate to PERCENTILE_DISC ?
  • How would you compute a median in an engine that has no percentile aggregate?
  • What do these functions return for an empty group or a group where every value is NULL?