Learn SQL Lesson
Combining Lambda Functions
Lambda functions can be nested. You can filter a list first, then transform or reduce the result.
For example, to sum only the prices above 10:
SELECT customer,
list_reduce(
list_filter(prices, lambda p : p > 10),
lambda a, b : a + b
) AS expensive_total
FROM orders
The inner `list_filter` keeps prices above 10, then `list_reduce` sums the survivors. If `list_filter` returns an empty list, `list_reduce` returns `NULL`.
You can also transform first and filter second:
SELECT list_filter(
list_transform([1, 2, 3, 4, 5], lambda x : x * x),
lambda sq : sq > 10
)
-- [16, 25]
Nesting gives you a mini data pipeline inside a single `SELECT` expression.
Practice challenge
Combine list_filter and list_reduce: for each customer, sum only the prices that are above 10. Return customer and the total as expensive_total, ordered by customer. Use COALESCE to return 0 when no prices qualify.