filter()

Constructs a list from those elements of the input list for which the lambda function returns true

Reference: DuckDB 1.3.2

Alias of list_filter()

Arguments & returns

filter(list, lambda)

Scalar
Overload link listlambda Returns
#01 ANY[]LAMBDA ANY[]

Example

filter example
SQL
Haybarn WASM 2.0.0-rc12 ยท In your browser

Engine example ยท Run to view results.

Catalog examples

SELECT
  list_filter([3, 4, 5], x -> x > 4);

In other engines

Used in larger rewrites 5

These examples use filter as one part of a larger SQL translation.

PostgreSQL

SELECT CORR(a, b) FILTER(WHERE c > 0)
In DuckDB
SELECT CORR(a, b) FILTER(WHERE c > 0)

SQLGlot example โ†—

Apache Hivecollect_list()

COLLECT_LIST(x)
In DuckDB
ARRAY_AGG(x) FILTER(WHERE x IS NOT NULL)

SQLGlot example โ†—

Apache Sparkcollect_set()

SELECT COLLECT_SET(sample_col) FROM sample_table
In DuckDB
SELECT LIST(DISTINCT sample_col) FILTER(WHERE NOT sample_col IS NULL) FROM sample_table

SQLGlot example โ†—

BigQueryarray_agg()

SELECT ARRAY_AGG(x IGNORE NULLS) AS x
In DuckDB
SELECT ARRAY_AGG(x) FILTER(WHERE x IS NOT NULL) AS x

SQLGlot example โ†—

Snowflakearray_agg()

SELECT ARRAY_AGG(DISTINCT a)
In DuckDB
SELECT ARRAY_AGG(DISTINCT a) FILTER(WHERE a IS NOT NULL)

SQLGlot example โ†—