---
title: percentile aggregate function
description: Learn the syntax of the percentile aggregate function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/percentile
---

# `percentile` aggregate function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the exact percentile value of `expr` at the specified `percentage` in a group.

## Syntax

```
percentile ( [ALL | DISTINCT] expr, percentage [, frequency] ) [FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: An expression that evaluates to a numeric.
* `percentage`: A numeric expression between 0 and 1 or an `ARRAY` of numeric expressions, each between 0 and 1.
* `frequency`: An optional integral number literal greater than 0.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

`DOUBLE` if `percentage` is numeric, or an `ARRAY<DOUBLE>` if `percentage` is an `ARRAY`.

Frequency describes the number of times `expr` must be counted. A frequency of 10 for a specific value is
equivalent to that value appearing 10 times in the window at a frequency of 1.
The default frequency is 1.

If `DISTINCT` is specified the function operates only on a unique set of `expr` values.

Databricks raises [DATATYPE\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/datatype-mismatch-error-class.md) if `percentage` is not between 0 and 1, or is not a constant.

Databricks raises [NEGATIVE\_VALUES\_IN\_FREQUENCY\_EXPRESSION](https://docs.databricks.com/aws/en/error-messages/error-classes.md#negative_values_in_frequency_expression) if `frequency` is negative.

## Common error conditions

* [DATATYPE\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/datatype-mismatch-error-class.md)
* [NEGATIVE\_VALUES\_IN\_FREQUENCY\_EXPRESSION](https://docs.databricks.com/aws/en/error-messages/error-classes.md#negative_values_in_frequency_expression)

## Examples

```sql
> SELECT percentile(col, 0.3) FROM VALUES (0), (10), (10) AS tab(col);
 6.0

> SELECT percentile(DISTINCT col, 0.3) FROM VALUES (0), (10), (10) AS tab(col);
 3.0

> SELECT percentile(col, 0.3, freq) FROM VALUES (0, 1), (10, 2) AS tab(col, freq);
 6.0

> SELECT percentile(col, array(0.25, 0.75)) FROM VALUES (0), (10) AS tab(col);
 [2.5,7.5]

> SELECT percentile(col, 1.5) FROM VALUES (1), (2) AS tab(col);
  Error: DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE
```

## Related functions

* [`approx_percentile` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/approx_percentile.md)
* [`histogram_numeric` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/histogram_numeric.md)
* [`percentile_approx` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/percentile_approx.md)
