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

Star Expression

## Examples

Select all columns present in the `FROM` clause:

    SELECT * FROM table_name;

Count the number of rows in a table:

    SELECT count(*) FROM table_name;

DuckDB offers a shorthand for `count(*)` expressions where the `*` may be omitted:

    SELECT count() FROM table_name;

Select all columns from the table called `table_name`:

    SELECT table_name.*
    FROM table_name
    JOIN other_table_name USING (id);

Select all columns except the city column from the addresses table:

    SELECT * EXCLUDE (city)
    FROM addresses;

Select all columns from the addresses table, but replace city with `lower(city)`:

    SELECT * REPLACE (lower(city) AS city)
    FROM addresses;

Select all columns matching the given expression:

    SELECT COLUMNS(c -> c LIKE '%num%')
    FROM addresses;

Select all columns matching the given regex from the table:

    SELECT COLUMNS('number\d+')
    FROM addresses;

Select columns using a list:

    SELECT COLUMNS(['city', 'zip_code'])
    FROM addresses;

## Syntax

## Star Expression

The `*` expression can be used in a `SELECT` statement to select all columns that are projected in the `FROM` clause.

    SELECT *
    FROM tbl;

The `*` expression can be modified using the `EXCLUDE` and `REPLACE`.

### `EXCLUDE` Clause

`EXCLUDE` allows us to exclude specific columns from the `*` expression.

    SELECT * EXCLUDE (col)
    FROM tbl;

### `REPLACE` Clause

`REPLACE` allows us to replace specific values in columns as specified by an expression.

    SELECT * REPLACE (col / 1_000 AS col)
    FROM tbl;

## `COLUMNS` Expression

The `COLUMNS` expression can be used to execute the same expression on the values in multiple columns. For example:

    CREATE TABLE numbers (id INTEGER, number INTEGER);
    INSERT INTO numbers VALUES (1, 10), (2, 20), (3, NULL);
    SELECT min(COLUMNS(*)), count(COLUMNS(*)) FROM numbers;

id | number | id | number
--- | --- | --- | ---
1 | 10 | 3 | 2

The `*` expression in the `COLUMNS` statement can also contain `EXCLUDE` or `REPLACE`, similar to regular star expressions.

    SELECT
        min(COLUMNS(* REPLACE (number + id AS number))),
        count(COLUMNS(* EXCLUDE (number)))
    FROM numbers;

id | min(number := (number + id)) | id
--- | --- | ---
1 | 11 | 3

`COLUMNS` expressions can also be combined, as long as the `COLUMNS` contains the same (star) expression:

    SELECT COLUMNS(*) + COLUMNS(*) FROM numbers;

id | number
--- | ---
2 | 20
4 | 40
6 | NULL

`COLUMNS` expressions can also be used in `WHERE` clauses. The conditions are applied to all columns and are combined using the logical `AND` operator.

    SELECT *
    FROM (
        SELECT 0 AS x, 1 AS y, 2 AS z
        UNION ALL
        SELECT 1 AS x, 2 AS y, 3 AS z
        UNION ALL
        SELECT 2 AS x, 3 AS y, 4 AS z
    )
    WHERE COLUMNS(*) > 1; -- equivalent to: x > 1 AND y > 1 AND z > 1

x | y | z
--- | --- | ---
2 | 3 | 4

## `COLUMNS` Regular Expression

`COLUMNS` supports passing a regex in as a string constant:

    SELECT COLUMNS('(id|numbers?)') FROM numbers;

id | number
--- | ---
1 | 10
2 | 20
3 | NULL

### Renaming Columns Using a `COLUMNS` Expression

The matches of capture groups can be used to rename columns selected by a regular expression. The capture groups are one-indexed; `\0` is the original column name.

For example, to select the first three letters of colum names, run:

    SELECT COLUMNS('(\w{3}).*') AS '\1' FROM numbers;

id | num
--- | ---
1 | 10
2 | 20
3 | NULL

To remove a colon (`:`) character in the middle of a column name, run:

    CREATE TABLE tbl ("Foo:Bar" INTEGER, "Foo:Baz" INTEGER, "Foo:Qux" INTEGER);
    SELECT COLUMNS('(\w*):(\w*)') AS '\1\2' FROM tbl;

## `COLUMNS` Lambda Function

`COLUMNS` also supports passing in a lambda function. The lambda function will be evaluated for all columns present in the `FROM` clause, and only columns that match the lambda function will be returned. This allows the execution of arbitrary expressions in order to select and rename columns.

    SELECT COLUMNS(c -> c LIKE '%num%') FROM numbers;

number
10
20
NULL

## `*COLUMNS` Unpacked Columns

The `*COLUMNS` clause is a variation of `COLUMNS`, which supports all of the previously mentioned capabilities. The difference is in how the expression expands.

`*COLUMNS` will expand in-place, much like the iterable unpacking behavior in Python, which inspired the `*` syntax. This implies that the expression expands into the parent expression. An example that shows this difference between `COLUMNS` and `*COLUMNS`:

With `COLUMNS`:

    SELECT coalesce(COLUMNS(['a', 'b', 'c'])) AS result
    FROM (SELECT NULL a, 42 b, true c);

result | result | result
--- | --- | ---
NULL | 42 | true

With `*COLUMNS`, the expression expands in its parent expression `coalesce`, resulting in a single result column:

    SELECT coalesce(*COLUMNS(['a', 'b', 'c'])) AS result
    FROM (SELECT NULL AS a, 42 AS b, true AS c);

result
42

`*COLUMNS` also works with the `(*)` argument:

    SELECT coalesce(*COLUMNS(*)) AS result
    FROM (SELECT NULL a, 42 AS b, true AS c);

result
42

## `STRUCT.*`

The `*` expression can also be used to retrieve all keys from a struct as separate columns. This is particularly useful when a prior operation creates a struct of unknown shape, or if a query must handle any potential struct keys. See the `STRUCT` data type and `STRUCT` functions pages for more details on working with structs.

For example:

    SELECT st.* FROM (SELECT {'x': 1, 'y': 2, 'z': 3} AS st);

x | y | z
--- | --- | ---
1 | 2 | 3

##### About this page

- Report content issue
- Edit this page on GitHub