Learn SQL Lesson

Filtering Lists with list_filter

`list_filter` keeps only the elements that satisfy a condition and returns a shorter list.

SELECT list_filter([10, 3, 25, 8], lambda x : x > 9)
-- [10, 25]

The `lambda` must return a boolean. Elements where it returns true are kept; the rest are dropped.

On a table column:

SELECT customer, list_filter(prices, lambda p : p > 10) AS expensive
FROM orders

If no elements match, the result is an empty list `[]`. This is useful for narrowing down list data before further processing.

Practice challenge

Use list_filter to keep only prices greater than 10. Return customer and the filtered list as high_prices, ordered by customer.

Open interactive editor