Skip to content

PERCENTILE_CONT

PERCENTILE_CONT( percentile ) WITHIN GROUP ( ORDER BY expr ) OVER ( [ query_partition_clause ] [ order_by_clause ] )

Section titled “PERCENTILE_CONT( percentile ) WITHIN GROUP ( ORDER BY expr ) OVER ( [ query_partition_clause ] [ order_by_clause ] )”
  • percentile: This is a number that specifies the percentile to compute. It must be a value between 0 and 1.
  • within group (order by expr): expr here represents the column or expression which is sorted to determine percentile values. It determines the order of the data. This clause is mandatory to define order for percentile calculations.
  • over ([query_partition_clause]): This portion of the statement allows you to partition the data into specific groups or chunks for calculated percentiles. The query_partition_clause allows you to specify how to partition rows into groups.
  • [order_by_clause]: This is an optional parameter which allows further ordering of your data within each partition or group. This means after partitioning using query_partition_clause, if we still have further criteria to arrange our data, then we use order_by_clause.
SELECT PERCENTILE_CONT(0.5)
WITHIN GROUP (ORDER BY salary) OVER () AS median
FROM employees;
MEDIAN
----------
60000

In the given example, the PERCENTILE_CONT function is used to calculate the median salary of all employees. Here, 0.5 represents the 50th percentile or median. The WITHIN GROUP clause sorts the salary in ascending order for this computation.