PERCENTILE_DISC()
PERCENTILE_DISC() is a SQL function in the Window Functions category. Discrete version of PERCENTILE_CONT. Returns an actual value from the data, not interpolated. The syntax is PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY column) OVER (PARTITION BY col). It accepts fraction. It returns discrete percentile value. A typical example: SELECT department, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary FROM employees GROUP BY department; -- Returns an actual salary value from the data (the one closest to 50th percentile) A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking. A close relative is DENSE_RANK(), which like RANK but without gaps. Equal values get the same rank, and the next number follows sequentially. More about this category: Window/analytical functions — ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, NTILE.