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

Utility Functions

## Scalar Utility Functions

The functions below are difficult to categorize into specific function types and are broadly useful.

Name | Description
--- | ---
`alias(column)` | Return the name of the column.
`checkpoint(database)` | Synchronize WAL with file for (optional) database without interrupting transactions.
`coalesce(expr, ...)` | Return the first expression that evaluates to a non-`NULL` value. Accepts 1 or more parameters. Each expression can be a column, literal value, function result, or many others.
`constant_or_null(arg1, arg2)` | If `arg2` is `NULL`, return `NULL`. Otherwise, return `arg1`.
`count_if(x)` | Aggregate function; rows contribute 1 if `x` is `true` or a non-zero number, else 0.
`current_catalog()` | Return the name of the currently active catalog. Default is memory.
`current_schema()` | Return the name of the currently active schema. Default is main.
`current_schemas(boolean)` | Return list of schemas. Pass a parameter of `true` to include implicit schemas.
`current_setting('setting_name')` | Return the current value of the configuration setting.
`currval('sequence_name')` | Return the current value of the sequence. Note that `nextval` must be called at least once prior to calling `currval`.
`error(message)` | Throws the given error `message`.
`equi_width_bins(min, max, bincount, nice := false)` | Returns the upper boundaries of a partition of the interval `[min, max]` into `bin_count` equal-sized subintervals (for use with, e.g., `histogram`). If `nice = true`, then `min`, `max`, and `bincount` may be adjusted to produce more aesthetically pleasing results.
`force_checkpoint(database)` | Synchronize WAL with file for (optional) database interrupting transactions.
`gen_random_uuid()` | Alias of `uuid`. Return a random UUID similar to this: `eeccb8c5-9943-b2bb-bb5e-222f4e14b687`.
`getenv(var)` | Returns the value of the environment variable `var`. Only available in the command line client.
`hash(value)` | Returns a `UBIGINT` with the hash of the `value`.
`icu_sort_key(string, collator)` | Surrogate key used to sort special characters according to the specific locale. Collator parameter is optional. Valid only when ICU extension is installed.
`if(a, b, c)` | Ternary conditional operator.
`ifnull(expr, other)` | A two-argument version of coalesce.
`is_histogram_other_bin(arg)` | Returns `true` when `arg` is the "catch-all element" of its datatype for the purpose of the `histogram_exact` function, which is equal to the "right-most boundary" of its datatype for the purpose of the `histogram` function.
`md5(string)` | Returns the MD5 hash of the `string` as a `VARCHAR`.
`md5_number(string)` | Returns the MD5 hash of the `string` as a `HUGEINT`.
`md5_number_lower(string)` | Returns the lower 64-bit segment of the MD5 hash of the `string` as a `BIGINT`.
`md5_number_higher(string)` | Returns the higher 64-bit segment of the MD5 hash of the `string` as a `BIGINT`.
`nextval('sequence_name')` | Return the following value of the sequence.
`nullif(a, b)` | Return `NULL` if `a = b`, else return `a`. Equivalent to `CASE WHEN a = b THEN NULL ELSE a END`.
`pg_typeof(expression)` | Returns the lower case name of the data type of the result of the expression. For PostgreSQL compatibility.
`query(``query_string_literal``)` | Table function that parses and executes the query defined in `query_string_literal`. Only literal strings are allowed. Warning: this function allows invoking arbitrary queries, potentially altering the database state.
`query_table(``tbl_name``)` | Table function that returns the table given in `tbl_name`.
`query_table(``tbl_names``, [``by_name``])` | Table function that returns the union of tables given in `tbl_names`. If the optional `by_name` parameter is set to `true`, it uses `UNION ALL BY NAME` semantics.
`read_blob(source)` | Returns the content from `source` (a filename, a list of filenames, or a glob pattern) as a `BLOB`. See the `read_blob` guide for more details.
`read_text(source)` | Returns the content from `source` (a filename, a list of filenames, or a glob pattern) as a `VARCHAR`. The file content is first validated to be valid UTF-8. If `read_text` attempts to read a file with invalid UTF-8 an error is thrown suggesting to use `read_blob` instead. See the `read_text` guide for more details.
`sha256(value)` | Returns a `VARCHAR` with the SHA-256 hash of the `value`.
`stats(expression)` | Returns a string with statistics about the expression. Expression can be a column, constant, or SQL expression.
`txid_current()` | Returns the current transaction's identifier, a `BIGINT` value. It will assign a new one if the current transaction does not have one already.
`typeof(expression)` | Returns the name of the data type of the result of the expression.
`uuid()` | Return a random UUID similar to this: `eeccb8c5-9943-b2bb-bb5e-222f4e14b687`.
`version()` | Return the currently active version of DuckDB in this format.

#### `alias(column)`

Description | Return the name of the column.
Example | `alias(column1)`
Result | `column1`

#### `checkpoint(database)`

Description | Synchronize WAL with file for (optional) database without interrupting transactions.
Example | `checkpoint(my_db)`
Result | success Boolean

#### `coalesce(expr, ...)`

Description | Return the first expression that evaluates to a non-`NULL` value. Accepts 1 or more parameters. Each expression can be a column, literal value, function result, or many others.
Example | `coalesce(NULL, NULL, 'default_string')`
Result | `default_string`

#### `constant_or_null(arg1, arg2)`

Description | If `arg2` is `NULL`, return `NULL`. Otherwise, return `arg1`.
Example | `constant_or_null(42, NULL)`
Result | `NULL`

#### `count_if(x)`

Description | Aggregate function; rows contribute 1 if `x` is `true` or a non-zero number, else 0.
Example | `count_if(42)`
Result | 1

#### `current_catalog()`

Description | Return the name of the currently active catalog. Default is memory.
Example | `current_catalog()`
Result | `memory`

#### `current_schema()`

Description | Return the name of the currently active schema. Default is main.
Example | `current_schema()`
Result | `main`

#### `current_schemas(boolean)`

Description | Return list of schemas. Pass a parameter of `true` to include implicit schemas.
Example | `current_schemas(true)`
Result | `['temp', 'main', 'pg_catalog']`

#### `current_setting('setting_name')`

Description | Return the current value of the configuration setting.
Example | `current_setting('access_mode')`
Result | `automatic`

#### `currval('sequence_name')`

Description | Return the current value of the sequence. Note that `nextval` must be called at least once prior to calling `currval`.
Example | `currval('my_sequence_name')`
Result | `1`

#### `error(message)`

Description | Throws the given error `message`.
Example | `error('access_mode')`

#### `equi_width_bins(min, max, bincount, nice := false)`

Description | Returns the upper boundaries of a partition of the interval `[min, max]` into `bin_count` equal-sized subintervals (for use with, e.g., `histogram`). If `nice = true`, then `min`, `max`, and `bincount` may be adjusted to produce more aesthetically pleasing results.
Example | `equi_width_bins(0.1, 2.7, 4, true)`
Result | `[0.5, 1.0, 1.5, 2.0, 2.5, 3.0]`

#### `force_checkpoint(database)`

Description | Synchronize WAL with file for (optional) database interrupting transactions.
Example | `force_checkpoint(my_db)`
Result | success Boolean

#### `gen_random_uuid()`

Description | Alias of `uuid`. Return a random UUID similar to this: `eeccb8c5-9943-b2bb-bb5e-222f4e14b687`.
Example | `gen_random_uuid()`
Result | various

#### `getenv(var)`

Description | Returns the value of the environment variable `var`. Only available in the command line client.
Example | `getenv('HOME')`
Result | `/path/to/user/home`

#### `hash(value)`

Description | Returns a `UBIGINT` with the hash of the `value`.
Example | `hash('🦆')`
Result | `2595805878642663834`

#### `icu_sort_key(string, collator)`

Description | Surrogate key used to sort special characters according to the specific locale. Collator parameter is optional. Valid only when ICU extension is installed.
Example | `icu_sort_key('ö', 'DE')`
Result | `460145960106`

#### `if(a, b, c)`

Description | Ternary conditional operator; returns b if a, else returns c. Equivalent to `CASE WHEN a THEN b ELSE c END`.
Example | `if(2 > 1, 3, 4)`
Result | `3`

#### `ifnull(expr, other)`

Description | A two-argument version of coalesce.
Example | `ifnull(NULL, 'default_string')`
Result | `default_string`

#### `is_histogram_other_bin(arg)`

Description | Returns `true` when `arg` is the "catch-all element" of its datatype for the purpose of the `histogram_exact` function, which is equal to the "right-most boundary" of its datatype for the purpose of the `histogram` function.
Example | `is_histogram_other_bin('')`
Result | `true`

#### `md5(string)`

Description | Returns the MD5 hash of the `string` as a `VARCHAR`.
Example | `md5('123')`
Result | `202cb962ac59075b964b07152d234b70`

#### `md5_number(string)`

Description | Returns the MD5 hash of the `string` as a `HUGEINT`.
Example | `md5_number('123')`
Result | `149263671248412135425768892945843956768`

#### `md5_number_lower(string)`

Description | Returns the lower 8 bytes of the MD5 hash of `string` as a `BIGINT`.
Example | `md5_number_lower('123')`
Result | `8091599832034528150`

#### `md5_number_higher(string)`

Description | Returns the higher 8 bytes of the MD5 hash of `string` as a `BIGINT`.
Example | `md5_number_higher('123')`
Result | `6559309979213966368`

#### `nextval('sequence_name')`

Description | Return the following value of the sequence.
Example | `nextval('my_sequence_name')`
Result | `2`

#### `nullif(a, b)`

Description | Return `NULL` if a = b, else return a. Equivalent to `CASE WHEN a = b THEN NULL ELSE a END`.
Example | `nullif(1+1, 2)`
Result | `NULL`

#### `pg_typeof(expression)`

Description | Returns the lower case name of the data type of the result of the expression. For PostgreSQL compatibility.
Example | `pg_typeof('abc')`
Result | `varchar`

#### `query(query_string_literal)`

Description | Table function that parses and executes the query defined in `query_string_literal`. Only literal strings are allowed. Warning: this function allows invoking arbitrary queries, potentially altering the database state.
Example | `query('SELECT 42 AS x')`
Result | `42`

#### `query_table(tbl_name)`

Description | Table function that returns the table given in `tbl_name`.
Example | `query('t1')`
Result | (the rows of `t1`)

#### `query_table(tbl_names, [by_name])`

Description | Table function that returns the union of tables given in `tbl_names`. If the optional `by_name` parameter is set to `true`, it uses `UNION ALL BY NAME` semantics.
Example | `query(['t1', 't2'])`
Result | (the union of the two tables)

#### `read_blob(source)`

Description | Returns the content from `source` (a filename, a list of filenames, or a glob pattern) as a `BLOB`. See the `read_blob` guide for more details.
Example | `read_blob('hello.bin')`
Result | `hello\x0A`

#### `read_text(source)`

Description | Returns the content from `source` (a filename, a list of filenames, or a glob pattern) as a `VARCHAR`. The file content is first validated to be valid UTF-8. If `read_text` attempts to read a file with invalid UTF-8 an error is thrown suggesting to use `read_blob` instead. See the `read_text` guide for more details.
Example | `read_text('hello.txt')`
Result | `hello\n`

#### `sha256(value)`

Description | Returns a `VARCHAR` with the SHA-256 hash of the `value`.
Example | `sha256('🦆')`
Result | `d7a5c5e0d1d94c32218539e7e47d4ba9c3c7b77d61332fb60d633dde89e473fb`

#### `stats(expression)`

Description | Returns a string with statistics about the expression. Expression can be a column, constant, or SQL expression.
Example | `stats(5)`
Result | `'[Min: 5, Max: 5][Has Null: false]'`

#### `txid_current()`

Description | Returns the current transaction's identifier, a `BIGINT` value. It will assign a new one if the current transaction does not have one already.
Example | `txid_current()`
Result | various

#### `typeof(expression)`

Description | Returns the name of the data type of the result of the expression.
Example | `typeof('abc')`
Result | `VARCHAR`

#### `uuid()`

Description | Return a random UUID similar to this: `eeccb8c5-9943-b2bb-bb5e-222f4e14b687`.
Example | `uuid()`
Result | various

#### `version()`

Description | Return the currently active version of DuckDB in this format.
Example | `version()`
Result | various

## Utility Table Functions

A table function is used in place of a table in a `FROM` clause.

Name | Description
--- | ---
`glob(search_path)` | Return filenames found at the location indicated by the search_path in a single column named `file`. The search_path may contain glob pattern matching syntax.
`repeat_row(varargs, num_rows)` | Returns a table with `num_rows` rows, each containing the fields defined in `varargs`.

#### `glob(search_path)`

Description | Return filenames found at the location indicated by the search_path in a single column named `file`. The search_path may contain glob pattern matching syntax.
Example | `glob('*')`
Result | (table of filenames)

#### `repeat_row(varargs, num_rows)`

Description | Returns a table with `num_rows` rows, each containing the fields defined in `varargs`.
Example | `repeat_row(1, 2, 'foo', num_rows = 3)`
Result | 3 rows of `1, 2, 'foo'`

##### About this page

- Report content issue
- Edit this page on GitHub