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

Last updated 13 July 2026

  • Ordered-set aggregates depend on input sort order, which is supplied with WITHIN GROUP (ORDER BY col).

  • PERCENTILE_CONT interpolates between the nearest rows, so it may return a value not in the data (e.g. the true median for an even count).

  • PERCENTILE_DISC returns the first actual value whose cumulative distribution reaches the fraction. Use it when the result must be a real data point.

  • https://support.teradata.com/community?id=community_blog&sys_id=9aa893671b9bfb00682ca8233a4bcb74
  • https://www.tigerdata.com/learn/understanding-percentile_cont-and-percentile_disc
  • https://dev.to/philip_mcclarence_2ef9475/postgresql-aggregate-and-window-function-tuning-4nbl
All Advanced SQL interview questions