---
title: ES|QL MEDIAN function
description: 
url: https://www.elastic.co/docs/reference/query-languages/esql/functions-operators/aggregation-functions/median
products:
  - Elasticsearch
---

# ES|QL MEDIAN function
## Syntax

![Embedded](https://www.elastic.co/docs/reference/query-languages/esql/images/generated/x-pack-esql/functions/median.svg)


## Parameters

<definitions>
  <definition term="number">
    Expression that outputs values to calculate the median of.
  </definition>
</definitions>


## Description

The value that is greater than half of all values and less than half of all values, also known as the 50% [`PERCENTILE`](https://www.elastic.co/docs/reference/query-languages/esql/functions-operators/aggregation-functions/percentile).
<note>
  Like [`PERCENTILE`](https://www.elastic.co/docs/reference/query-languages/esql/functions-operators/aggregation-functions/percentile), `MEDIAN` is [usually approximate](/docs/reference/query-languages/esql/functions-operators/aggregation-functions/percentile#esql-percentile-approximate).
</note>


## Supported types


| number                                                                                                                     | result |
|----------------------------------------------------------------------------------------------------------------------------|--------|
| double                                                                                                                     | double |
| exponential_histogram <applies-to>Elastic Stack: Generally available since 9.4, Elastic Stack: Preview in 9.3</applies-to> | double |
| integer                                                                                                                    | double |
| long                                                                                                                       | double |
| tdigest <applies-to>Elastic Stack: Generally available since 9.4</applies-to>                                              | double |


## Examples

```esql
FROM employees
| STATS MEDIAN(salary), PERCENTILE(salary, 50)
```


| MEDIAN(salary):double | PERCENTILE(salary, 50):double |
|-----------------------|-------------------------------|
| 47003                 | 47003                         |

The expression can use inline functions. For example, to calculate the median of the maximum values of a multivalued column, first use `MV_MAX` to get the maximum value per row, and use the result with the `MEDIAN` function
```esql
FROM employees
| STATS median_max_salary_change = MEDIAN(MV_MAX(salary_change))
```


| median_max_salary_change:double |
|---------------------------------|
| 7.69                            |

`MEDIAN` can also operate on `exponential_histogram` fields, approximating the median of the values which were used to construct the histograms.
```esql
TS exp_histo_sample
| WHERE instance == "instance-0"
| STATS p50 = ROUND(MEDIAN(responseTime), 4)
```


| p50:double |
|------------|
| 0.0211     |

`MEDIAN` can also operate on `tdigest` and casted `histogram` fields, approximating the median of the values which were used to construct the digests.
```esql
TS histogram_timeseries_index
| WHERE instance == "instance-0"
| STATS p50 = ROUND(MEDIAN(responseTime::tdigest), 2)
```


| p50:double |
|------------|
| 0.02       |

<warning>
  `MEDIAN` is also [non-deterministic](https://en.wikipedia.org/wiki/Nondeterministic_algorithm).
  This means you can get slightly different results using the same data.
</warning>