SQL PERCENTILE_CONT

The interpolated percentile — when the exact percentile falls between two values, it computes a point in between.

What & Why

PERCENTILE_CONT(p) treats the data as continuous — if the target percentile lands between two actual values, it returns a weighted point between them, not necessarily a value that exists in the data.

See How It Works

BUSINESS QUESTION

Calculate continuous median and 90th-percentile campaign spend by channel.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  channel,
  ROUND(PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY spend)::numeric, 2) AS median_spend,
  ROUND(PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY spend)::numeric, 2) AS p90_spend
FROM marketing.campaigns
GROUP BY channel
ORDER BY channel;
RESULT — continuous campaign percentiles
channelmedian_spendp90_spend
email45000.0045000.00
google_ads57500.0059500.00
linkedin50000.0050000.00

Continuous percentiles interpolate within the two-row google_ads distribution.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario