> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PERCENTILE_APPROX

> Estimates a percentile or an ordered array of percentiles, with accuracy controlled by the accuracy argument.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Aggregate · Window</span></div>

NULL values are ignored. An empty input returns NULL, whether requesting one percentile or an array of percentiles.

## Syntax

A window function keeps each input row and adds a result for that row. `PARTITION BY` groups rows with matching values; without it, all input rows form one group.

### Aggregate

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
percentile_approx(value, percentile [, accuracy])
```

* Requires native execution and a source database context other than PRESTO.

### Window

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
percentile_approx(value, percentile [, accuracy]) OVER ([PARTITION BY key] [ORDER BY ordering_key] [ROWS frame])
```

* Requires native execution and a source database context other than PRESTO.
* Use a frame that includes all rows in the group or starts at the first row (`UNBOUNDED PRECEDING`). Sliding frames that drop earlier rows are not supported. RANGE frames with a numeric or time offset are not supported.

## Arguments

### Aggregate and window

| Name | Type | Required | Description |
| - | - | - | - |
| value | TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE or DECIMAL(p, s) | Yes | Values to estimate. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| percentile | TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE or DECIMAL(p, s); or ARRAY\<F> | Yes | Fraction(s) in \[0,1]. F is TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE or DECIMAL(p, s). Every scalar or array element must be a constant fraction in \[0, 1]; fractions are converted to the floating-point form. Must be a constant argument. |
| accuracy | positive INTEGER constant | No | Controls accuracy. Higher values use more memory. Must be a positive constant integer. Default: 10000. |

## Returns

* Type: same type as value, or ARRAY\<same type as value>
* NULL behavior: NULL values are ignored. An empty input returns NULL, whether requesting one percentile or an array of percentiles.
* Empty input: Returns NULL on empty input.
* Shape: Scalar fraction returns scalar; array fractions return an array of matching length.
* Ordering: Array result order follows requested fractions.

## Properties

| Property | Value |
| - | - |
| Deterministic | Partial (Estimated percentiles can vary with the order in which values are processed.) |
| Supports DISTINCT | Partial (Removes duplicates in grouped aggregation only.) |
| Supports window usage | Partial (The frame must start at the first row in the group. Requires a source context other than PRESTO.) |
| Supports window frames | Partial (The frame must start at the first row in the group. Requires a source context other than PRESTO.) |
| Exact result | No |

* Approximation bound: Relative rank error is 1/accuracy; default accuracy is 10000. This is a rank-error statement, not an absolute numeric-value error bound.

## Usage notes

* Fraction and accuracy must be constant expressions. They cannot vary by row.
* Higher accuracy uses more memory. Approximate results can vary with the order in which values are processed.
* INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
* DISTINCT removes duplicates in grouped aggregation. Do not use it to remove duplicates in a window call.

## Examples

### Median estimate

* Requires native execution and a source database context other than PRESTO.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH t("value") AS (VALUES (1),(2),(3)) SELECT percentile_approx("value",0.5) AS result FROM t;
```

| result |
| - |
| 2 |

### Estimate the upper quartile

This small dataset gives the result shown; percentile estimates can differ from an exact percentile calculation.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH sample(v) AS (VALUES (10), (20), (30), (40), (50))
SELECT percentile_approx(v, 0.75) AS upper_quartile FROM sample;
```

| upper\_quartile |
| - |
| 40 |

## Limitations

* Sliding windows that drop earlier rows are not supported. Start the frame at the first row in the group.
* PERCENTILE\_APPROX is available with native execution outside the PRESTO source context. It is not available with Java execution.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.