MEDIAN Function
Description
The MEDIAN function computes the median of a numeric column. The median is the middle value when the data is sorted in order. This function is equivalent to percentile(expr, 0.5), i.e., computing the 50th percentile.
Parameters
expr: A numeric expression, which can be of integer, floating-point, orDECIMALtype.
Return Type
- Returns a
DOUBLEvalue. - Returns the median of the dataset.
Notes
- If the input is an empty set, returns
NULL. - For an even number of values, returns the average of the two middle values.
- For an odd number of values, returns the exact middle value.
NULLvalues are automatically ignored and excluded from computation.MEDIANdoes not support theFILTERclause. For conditional filtering, use aWHEREclause at the outer level or switch topercentile(expr, 0.5) FILTER (...).
Examples
- Basic usage: compute the median
- Median of an even number of values (average of the two middle values)
- Median of an odd number of values
- Compute the median by group
