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

# FILTER

> Keeps array elements whose lambda returns TRUE.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Scalar function</span></div>

**Aliases:** [`filter_array`](/query/sql/functions/array/filter_array)

NULL array returns NULL; NULL elements are passed to the lambda. FALSE and NULL predicates discard the element.

## Syntax

### Filter form (scalar)

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
filter(array,lambda)
```

## Arguments

Type notation used below:

* T is the array element type: BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT.

| Name | Type | Required | Description |
| - | - | - | - |
| array | ARRAY\<T> | Yes | Input elements. Nested elements/fields must satisfy the same element-type restrictions. |
| lambda | FUNCTION(T \[, BIGINT]) -> BOOLEAN | Yes | Receives element and optional zero-based index. Nested elements/fields must satisfy the same element-type restrictions. |

## Returns

* Type: ARRAY\<T>
* Possible values: Retained input elements.
* NULL behavior: NULL array returns NULL; NULL elements are passed to the lambda. FALSE and NULL predicates discard the element.
* Shape: Zero through input number of elements.
* Ordering: Input encounter order.

## Properties

| Property | Value |
| - | - |
| Deterministic | Partial (Deterministic when the lambda and its referenced expressions are deterministic.) |
| Supports window usage | No |

## Usage notes

* This function filters elements within an array. The aggregate clause `FILTER (WHERE ...)` is a separate SQL feature.

* The optional lambda index is zero-based.

* The optional lambda index is BIGINT and starts at 0 for each input array.

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

## Examples

### Typical behavior

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT filter(ARRAY[1, 2, 3], x -> x > 1) AS "value";
```

| value |
| - |
| \[2,3] |

### Filter strings

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT filter(ARRAY('ant', 'bear', 'cat'), x -> length(x) > 3) AS long_words;
```

| long\_words |
| - |
| \["bear"] |

## Limitations

* The same input gives repeatable results only when the supplied lambda does. Avoid random or other changing expressions when you need reproducible results.


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