## PERCENTILE

The `PERCENTILE` function in ES|QL returns the value at which a certain percentage of observed values occur. For example, the 95th percentile is the value which is greater than 95% of the observed values and the 50th percentile is the median.

### Syntax

`PERCENTILE(expression, percentile)`

#### Parameters

- `expression`: Expression from which to return a percentile.
- `percentile`: A constant numeric expression.

### Examples

Here are a couple of examples of how to use the `PERCENTILE` function in ES|QL:

```esql
FROM employees
| STATS p0 = PERCENTILE(salary,  0), p50 = PERCENTILE(salary, 50), p99 = PERCENTILE(salary, 99)
```

In this example, the `PERCENTILE` function is used to calculate the 0th, 50th, and 99th percentiles of the `salary` field in the `employees` index.

```esql
FROM employees
| STATS p80_max_salary_change = PERCENTILE(MV_MAX(salary_change), 80)
```

In this example, the `PERCENTILE` function is used in conjunction with the `MV_MAX` function to calculate the 80th percentile of the maximum values of the `salary_change` field in the `employees` index.

### Note

The `PERCENTILE` function is usually approximate. There are many different algorithms to calculate percentiles and the naive implementation does not scale. To calculate percentiles across potentially billions of values in an Elasticsearch cluster, approximate percentiles are calculated using the TDigest algorithm. This means you can get slightly different results using the same data.