Search Shortcut cmd + k | ctrl + k

- Installation
- Documentation

- Getting Started
- Connect

- Overview
- Concurrency

- Data Import

- Overview
- Data Sources
- CSV Files

- Overview
- Auto Detection
- Reading Faulty CSV Files
- Tips

- JSON Files

- Overview
- Creating JSON
- Loading JSON
- Writing JSON
- JSON Type
- JSON Functions
- Format Settings
- Installing and Loading
- SQL to / from JSON
- Caveats

- Multiple Files

- Overview
- Combining Schemas

- Parquet Files

- Overview
- Metadata
- Encryption
- Tips

- Partitioning

- Hive Partitioning
- Partitioned Writes

- Appender
- INSERT Statements

- Client APIs

- Overview
- C

- Overview
- Startup
- Configuration
- Query
- Data Chunks
- Vectors
- Values
- Types
- Prepared Statements
- Appender
- Table Functions
- Replacement Scans
- API Reference

- C++
- CLI

- Overview
- Arguments
- Dot Commands
- Output Formats
- Editing
- Autocomplete
- Syntax Highlighting

- Dart
- Go
- Java
- Julia
- Node.js (Neo)

- Overview

- Node.js

- Overview
- API Reference

- Python

- Overview
- Data Ingestion
- Conversion between DuckDB and Python
- DB API
- Relational API
- Function API
- Types API
- Expression API
- Spark API
- API Reference
- Known Python Issues

- R
- Rust
- Swift
- Wasm

- Overview
- Instantiation
- Data Ingestion
- Query
- Extensions

- ADBC
- ODBC

- Overview
- Linux Setup
- Windows Setup
- macOS Setup
- Configuration

- SQL

- Introduction
- Statements

- Overview
- ANALYZE
- ALTER TABLE
- ALTER VIEW
- ATTACH and DETACH
- CALL
- CHECKPOINT
- COMMENT ON
- COPY
- CREATE INDEX
- CREATE MACRO
- CREATE SCHEMA
- CREATE SECRET
- CREATE SEQUENCE
- CREATE TABLE
- CREATE VIEW
- CREATE TYPE
- DELETE
- DESCRIBE
- DROP
- EXPORT and IMPORT DATABASE
- INSERT
- LOAD / INSTALL
- PIVOT
- Profiling
- SELECT
- SET / RESET
- SET VARIABLE
- SUMMARIZE
- Transaction Management
- UNPIVOT
- UPDATE
- USE
- VACUUM

- Query Syntax

- SELECT
- FROM and JOIN
- WHERE
- GROUP BY
- GROUPING SETS
- HAVING
- ORDER BY
- LIMIT and OFFSET
- SAMPLE
- Unnesting
- WITH
- WINDOW
- QUALIFY
- VALUES
- FILTER
- Set Operations
- Prepared Statements

- Data Types

- Overview
- Array
- Bitstring
- Blob
- Boolean
- Date
- Enum
- Interval
- List
- Literal Types
- Map
- NULL Values
- Numeric
- Struct
- Text
- Time
- Timestamp
- Time Zones
- Union
- Typecasting

- Expressions

- Overview
- CASE Statement
- Casting
- Collations
- Comparisons
- IN Operator
- Logical Operators
- Star Expression
- Subqueries

- Functions

- Overview
- Aggregate Functions
- Array Functions
- Bitstring Functions
- Blob Functions
- Date Format Functions
- Date Functions
- Date Part Functions
- Enum Functions
- Interval Functions
- Lambda Functions
- List Functions
- Map Functions
- Nested Functions
- Numeric Functions
- Pattern Matching
- Regular Expressions
- Struct Functions
- Text Functions
- Time Functions
- Timestamp Functions
- Timestamp with Time Zone Functions
- Union Functions
- Utility Functions
- Window Functions

- Constraints
- Indexes
- Meta Queries

- Information Schema
- Metadata Functions

- DuckDB's SQL Dialect

- Overview
- Indexing
- Friendly SQL
- Keywords and Identifiers
- Order Preservation
- PostgreSQL Compatibility

- Samples

- Configuration

- Overview
- Pragmas
- Secrets Manager

- Extensions

- Overview
- Core Extensions
- Community Extensions
- Working with Extensions
- Versioning of Extensions
- Arrow
- AutoComplete
- AWS
- Azure
- Delta
- Excel
- Full Text Search
- httpfs (HTTP and S3)

- Overview
- HTTP(S) Support
- Hugging Face
- S3 API Support
- Legacy Authentication Scheme for S3 API

- Iceberg
- ICU
- inet
- jemalloc
- MySQL
- PostgreSQL
- Spatial

- Overview
- Function Reference
- R-Tree Indexes
- GDAL Integration

- SQLite
- Substrait
- TPC-DS
- TPC-H
- VSS

- Guides

- Overview
- Data Viewers

- Tableau
- CLI Charting with YouPlot

- Database Integration

- Overview
- MySQL Import
- PostgreSQL Import
- SQLite Import

- File Formats

- Overview
- CSV Import
- CSV Export
- Directly Reading Files
- Excel Import
- Excel Export
- JSON Import
- JSON Export
- Parquet Import
- Parquet Export
- Querying Parquet Files

- Network and Cloud Storage

- Overview
- HTTP Parquet Import
- S3 Parquet Import
- S3 Parquet Export
- S3 Iceberg Import
- S3 Express One
- GCS Import
- Cloudflare R2 Import
- DuckDB over HTTPS / S3

- Meta Queries

- Describe Table
- EXPLAIN: Inspect Query Plans
- EXPLAIN ANALYZE: Profile Queries
- List Tables
- Summarize
- DuckDB Environment

- ODBC

- ODBC Guide

- Performance

- Overview
- Environment
- Import
- Schema
- Indexing
- Join Operations
- File Formats
- How to Tune Workloads
- My Workload Is Slow
- Benchmarks

- Python

- Installation
- Executing SQL
- Jupyter Notebooks
- SQL on Pandas
- Import from Pandas
- Export to Pandas
- Import from Numpy
- Export to Numpy
- SQL on Arrow
- Import from Arrow
- Export to Arrow
- Relational API on Pandas
- Multiple Python Threads
- Integration with Ibis
- Integration with Polars
- Using fsspec Filesystems

- SQL Editors

- DBeaver SQL IDE

- SQL Features

- AsOf Join
- Full-Text Search
- query and query_table Functions

- Snippets

- Create Synthetic Data
- Sharing Macros

- Glossary of Terms
- Browse Offline

- Operations Manual

- Overview
- Limits
- Non-Deterministic Behavior
- Embedding DuckDB
- DuckDB's Footprint

- Files Created by DuckDB
- Gitignore for DuckDB
- Reclaiming Space

- Securing DuckDB

- Overview
- Securing Extensions

- Development

- DuckDB Repositories
- Testing

- Overview
- sqllogictest Introduction
- Writing Tests
- Debugging
- Result Verification
- Persistent Testing
- Loops
- Multiple Connections
- Catch

- Profiling
- Release Calendar
- Building

- Overview
- Build Instructions
- Build Configuration
- Building Extensions
- Platforms
- Troubleshooting

- Benchmark Suite

- Internals

- Overview
- Storage Versions and Format
- Execution Format
- Pivot

- Sitemap
- Why DuckDB
- Media
- FAQ
- Code of Conduct
- Live Demo

Lambda Functions

Lambda functions enable the use of more complex and flexible expressions in queries. DuckDB supports several scalar functions that operate on `LIST`s and accept lambda functions as parameters in the form `(parameter1, parameter2, ...) -> expression`. If the lambda function has only one parameter, then the parentheses can be omitted. The parameters can have any names. For example, the following are all valid lambda functions:

- `param -> param > 1`
- `s -> contains(concat(s, 'DB'), 'duck')`
- `(x, y) -> x + y`

## Scalar Functions That Accept Lambda Functions

Name | Description
--- | ---
`list_transform(list, lambda)` | Returns a list that is the result of applying the lambda function to each element of the input list.
`list_filter(list, lambda)` | Constructs a list from those elements of the input list for which the lambda function returns `true`.
`list_reduce(list, lambda)` | Reduces all elements of the input list into a single value by executing the lambda function on a running result and the next list element. The list must have at least one element – the use of an initial accumulator value is currently not supported.

### `list_transform(list, lambda)`

Description | Returns a list that is the result of applying the lambda function to each element of the input list. For more information, see Transform.
Example | `list_transform([4, 5, 6], x -> x + 1)`
Result | `[5, 6, 7]`
Aliases | `array_transform`, `apply`, `list_apply`, `array_apply`

### `list_filter(list, lambda)`

Description | Constructs a list from those elements of the input list for which the lambda function returns `true`. For more information, see Filter.
Example | `list_filter([4, 5, 6], x -> x > 4)`
Result | `[5, 6]`
Aliases | `array_filter`, `filter`

### `list_reduce(list, lambda)`

Description | Reduces all elements of the input list into a single value by executing the lambda function on a running result and the next list element. The list must have at least one element – the use of an initial accumulator value is currently not supported. For more information, see Reduce.
Example | `list_reduce([4, 5, 6], (x, y) -> x + y)`
Result | `15`
Aliases | `array_reduce`, `reduce`

## Nesting

All scalar functions can be arbitrarily nested.

Nested lambda functions to get all squares of even list elements:

    SELECT list_transform(
            list_filter([0, 1, 2, 3, 4, 5], x -> x % 2 = 0),
            y -> y * y
        );

    [0, 4, 16]

Nested lambda function to add each element of the first list to the sum of the second list:

    SELECT list_transform(
            [1, 2, 3],
            x -> list_reduce([4, 5, 6], (a, b) -> a + b) + x
        );

    [16, 17, 18]

## Scoping

Lambda functions confirm to scoping rules in the following order:

- inner lambda parameters
- outer lambda parameters
- column names
- macro parameters

    CREATE TABLE tbl (x INTEGER);
    INSERT INTO tbl VALUES (10);
    SELECT apply([1, 2], x -> apply([4], x -> x + tbl.x)[1] + x) FROM tbl;

    [15, 16]

## Indexes as Parameters

All lambda functions accept an optional extra parameter that represents the index of the current element. This is always the last parameter of the lambda function, and is 1-based (i.e., the first element has index 1).

Get all elements that are larger than their index:

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

    [3, 5]

## Transform

Signature: `list_transform(list, lambda)`

Description: `list_transform` returns a list that is the result of applying the lambda function to each element of the input list.

Aliases:

- `array_transform`
- `apply`
- `list_apply`
- `array_apply`

Number of parameters excluding indexes: 1

Return type: Defined by the return type of the lambda function

### Examples

Incrementing each list element by one:

    SELECT list_transform([1, 2, NULL, 3], x -> x + 1);

    [2, 3, NULL, 4]

Transforming strings:

    SELECT list_transform(['Duck', 'Goose', 'Sparrow'], s -> concat(s, 'DB'));

    [DuckDB, GooseDB, SparrowDB]

Combining lambda functions with other functions:

    SELECT list_transform([5, NULL, 6], x -> coalesce(x, 0) + 1);

    [6, 1, 7]

## Filter

Signature: `list_filter(list, lambda)`

Description: Constructs a list from those elements of the input list for which the lambda function returns `true`. DuckDB must be able to cast the lambda function's return type to `BOOL`.

Aliases:

- `array_filter`
- `filter`

Number of parameters excluding indexes: 1

Return type: The same type as the input list

### Examples

Filter out negative values:

    SELECT list_filter([5, -6, NULL, 7], x -> x > 0);

    [5, 7]

Divisible by 2 and 5:

    SELECT list_filter(
            list_filter([2, 4, 3, 1, 20, 10, 3, 30], x -> x % 2 = 0),
            y -> y % 5 = 0
        );

    [20, 10, 30]

In combination with `range(...)` to construct lists:

    SELECT list_filter([1, 2, 3, 4], x -> x > #1) FROM range(4);

    [1, 2, 3, 4]
    [2, 3, 4]
    [3, 4]
    [4]
    []

## Reduce

Signature: `list_reduce(list, lambda)`

Description: The scalar function returns a single value that is the result of applying the lambda function to each element of the input list. Starting with the first element and then repeatedly applying the lambda function to the result of the previous application and the next element of the list. The list must have at least one element.

Aliases:

- `array_reduce`
- `reduce`

Number of parameters excluding indexes: 2

Return type: The type of the input list's elements

### Examples

Sum of all list elements:

    SELECT list_reduce([1, 2, 3, 4], (x, y) -> x + y);

    10

Only add up list elements if they are greater than 2:

    SELECT list_reduce(list_filter([1, 2, 3, 4], x -> x > 2), (x, y) -> x + y);

    7

Concat all list elements:

    SELECT list_reduce(['DuckDB', 'is', 'awesome'], (x, y) -> concat(x, ' ', y));

    DuckDB is awesome

##### About this page

- Report content issue
- Edit this page on GitHub