MEDIAN()
MEDIAN() is a SQL function in the Aggregate Functions category. Oracle-specific: median value of a dataset. PostgreSQL uses PERCENTILE_CONT(0.5) WITHIN GROUP. SQL Server uses PERCENTILE_CONT. The syntax is MEDIAN(column). It accepts column. It returns median value. A typical example: -- Oracle: SELECT MEDIAN(salary) AS median_salary FROM employees; SELECT department, MEDIAN(salary) AS dept_median FROM employees GROUP BY department; -- PostgreSQL/SQL Server equivalent: SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) FROM employees; -- MySQL: no built-in MEDIAN -- Workaround using variables or window functions: SELECT AVG(salary) AS median FROM ( SELECT salary, @row:=@row+1 AS row_num, @count:=@row AS total FROM employees ORDER BY salary ) AS sorted; -- More complex than other… A close relative is COUNT(), which returns the number of rows (or non-NULL values) in a group. A close relative is SUM(), which returns the sum of all non-NULL values in a group. Works with numeric columns. A close relative is AVG(), which returns the average (mean) of all non-NULL values in a group.