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

> Reference material for MEDIAN

# MEDIAN

Returns the middle value in a given column. If number of values are even, `MEDIAN` returns the average of the two middle values.

## Syntax

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
MEDIAN(<value>) [FILTER ([WHERE] <condition>)]
```

## Parameters

| Parameter     | Description                                                       | Supported input types                       |
| :------------ | :---------------------------------------------------------------- | :------------------------------------------ |
| `<value>`     | The expression used to calculate the median value.                | `DOUBLE PRECISION`, `REAL`, `BIGINT`, `INT` |
| `<condition>` | An optional boolean expression to filter rows used in aggregation | `BOOL`                                      |

## Return Type

`MEDIAN` returns a value of type `DOUBLE PRECISION`.

* This function ignores `NULL` values.
* If the input is empty, the function returns `NULL`.

## Examples

The following example calculates the median of three values. Since the number of elements is odd, the middle element is returned directly:

<div className="query-window">
  ```
  SELECT MEDIAN(a) AS result FROM UNNEST(array[1,2,5]) AS a;
  ```

  | result <span>double null</span> |
  | :------------------------------ |
  | 2                               |

  <p><span>Rows: 1</span><span>Execution time: 6.04ms</span></p>
</div>

When the number of non-`NULL` elements is even, `MEDIAN` returns the average of the two middle values. `NULL` values are ignored before the median is calculated:

<div className="query-window">
  ```
  SELECT MEDIAN(a) AS result FROM UNNEST(array[100, NULL, 1, 2, 5]) AS a;
  ```

  | result <span>double null</span> |
  | :------------------------------ |
  | 3.5                             |

  <p><span>Rows: 1</span><span>Execution time: 6.46ms</span></p>
</div>

After sorting and ignoring `NULL`, the non-null values are `[1, 2, 5, 100]`. The two middle elements are `2` and `5`, so the result is calculated as: `((2+5)/2)`.
