> ## 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.

# MEDIAN

> Returns the interpolated 50th percentile.

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

NULL values are ignored; all-NULL input yields NULL.

## 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"}}
median([DISTINCT] value)
```

### Window

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

## Arguments

### Aggregate and window

| Name | Type | Required | Description |
| - | - | - | - |
| value | TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE or DECIMAL(p, s) | Yes | Values whose median is calculated. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |

## Returns

* Type: DOUBLE for integer inputs; the input’s effective FLOAT, DOUBLE, or DECIMAL type for floating-point and decimal inputs
* NULL behavior: NULL values are ignored; all-NULL input yields NULL.
* Empty input: Returns NULL on empty input.

## Properties

| Property | Value |
| - | - |
| Deterministic | Partial (Floating-point conversion and interpolation can round results, especially for large numbers.) |
| Supports DISTINCT | Partial (Removes duplicates in grouped aggregation; window calls still include duplicates.) |
| Supports window usage | Partial (Sliding ROWS frames work without DISTINCT. RANGE offsets are not supported.) |
| Supports window frames | Partial (Sliding ROWS frames are supported for the window syntax shown above.) |
| Exact result | Partial (Floating-point arithmetic can round.) |

## Usage notes

* MEDIAN calculates the continuous percentile at 0.5.
* MEDIAN calculates percentile 0.5 and returns one value. It does not accept an array of percentile fractions.
* 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.
* In grouped aggregation, DISTINCT removes duplicate input values before calculating the median.

## Examples

### Basic example

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

| result |
| - |
| 2.5 |

### Groups with different numbers of values

NULL values are excluded from the calculation. Results are rounded to six decimal places where needed.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
WITH sample(grp, v) AS (VALUES (1, 1), (1, 5), (2, 8), (2, CAST(NULL AS INT)))
SELECT grp, round(median(CAST(v AS DOUBLE)), 6) AS result
FROM sample GROUP BY grp ORDER BY grp;
```

| grp | result |
| - | - |
| 1 | 3 |
| 2 | 8 |

## Limitations

* Sliding ROWS frames are supported for the window syntax shown above. RANGE frames with numeric or time offsets are not supported.
* Floating-point and decimal results can keep their effective input type, even when query tools report DOUBLE.
* DISTINCT in a window call currently has no effect: duplicate rows still contribute to the result. Use grouped DISTINCT aggregation when duplicate removal is required.


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