Learn SQL Lesson
Transforming Lists with list_transform
`list_transform` applies a function to every element of a list and returns a new list with the results. Think of it like a map operation.
The syntax uses a `lambda` expression:
SELECT list_transform([1, 2, 3], lambda x : x * 10)
-- [10, 20, 30]
Lambda expressions start with the keyword `lambda`, followed by a parameter name, a colon, and the expression to evaluate.
You can use this on table columns too. For example, adding 10% tax to every price in a list:
SELECT customer, list_transform(prices, lambda p : round(p * 1.1, 2)) AS taxed
FROM orders
`NULL` elements stay `NULL` after transformation. The return type of the new list is determined by the lambda expression.
The sample table stores `items` and `prices` as parallel lists. That means the first item belongs with the first price, the second item belongs with the second price, and so on. This is compact, but position-dependent data can be fragile if the lists get out of sync.
Practice challenge
Use list_transform to double every price in the prices column. Return customer and the transformed list as doubled_prices, ordered by customer.