MEDIAN
Example
Section titled “Example”SELECT MEDIAN(salary) OVER () AS median_salaryFROM employees;Output
Section titled “Output”MEDIAN_SALARY--------------5000.0Explanation
Section titled “Explanation”This query is computing the median of all the values present in the salary column from the employees table. The median is the middle value in an ordered list of numbers. Here, the median salary is 5000.0.
Example
Section titled “Example”SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY salary) AS medianFROM employees;Output
Section titled “Output” median-------- 52000(1 row)Explanation
Section titled “Explanation”The function percentile_cont(0.5) calculates the median of the ‘salary’ column. The WITHIN GROUP (ORDER BY salary) portion sorts the salaries in ascending order. The calculated median of ‘salary’ is then returned, which in this example is 52000.