Star expressions
* expands to every column of the FROM clause, and table.* expands to every column of one table or table alias. Add modifiers to change the expansion. EXCLUDE leaves out the listed columns. REPLACE keeps every column but replaces the values of the listed columns with expressions. RENAME keeps the values and changes the column names. LIKE, ILIKE, GLOB, and SIMILAR TO keep only the columns whose names match a pattern.
COLUMNS(selector) expands to the columns that the selector chooses. The selector can be a regular expression that the column names must match, a list of column names, a lambda that tests each column name, or a star expression. When you pass COLUMNS to a function, the function is applied to each chosen column, and the result has one column for each. In an alias for such an expression, \0 stands for the name of the original column.
To pass the chosen columns to one function call as separate arguments, write *COLUMNS(selector).
Syntax
[ table. ]* [ EXCLUDE ( column [, ...] ) ]
[ REPLACE ( expression AS column [, ...] ) ]
[ RENAME ( column AS new_name [, ...] ) ]
* { LIKE | ILIKE | GLOB | SIMILAR TO } 'pattern'
COLUMNS( { 'regex' | [ 'column'
[, ...] ] | lambda c: condition
| star_expression } )
*COLUMNS( selector )
Examples
For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.
The following SQL query returns every column except customer and order_date.
SELECT * EXCLUDE (customer, order_date) FROM sales_db.sales_parquet WHERE order_id <= 2 ORDER BY order_id
The query returns the following results.
| order_id | region | amount |
|---|---|---|
|
|
|
|
|
|
The following SQL query returns every column, with the region values in uppercase.
SELECT * REPLACE (upper(region) AS region) FROM sales_db.sales_parquet WHERE order_id <= 2 ORDER BY order_id
The query returns the following results.
| order_id | customer | region | amount | order_date |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
The following SQL query returns every column and renames customer to buyer.
SELECT * RENAME (customer AS buyer) FROM sales_db.sales_parquet WHERE order_id = 1
The query returns the following result.
| order_id | buyer | region | amount | order_date |
|---|---|---|---|---|
|
|
|
|
|
The following SQL query returns only the columns whose names start with order.
SELECT * LIKE 'order%' FROM sales_db.sales_parquet WHERE order_id = 1
The query returns the following result.
| order_id | order_date |
|---|---|
|
|
The following SQL query applies max to two columns that a regular expression chooses, and names each result after its column.
SELECT max(COLUMNS('order_id|amount')) AS 'max_\0' FROM sales_db.sales_parquet
The query returns the following result.
| max_order_id | max_amount |
|---|---|
|
|
The following SQL query uses a lambda to keep the columns whose names match the pattern '%_id', plus the amount column.
SELECT COLUMNS(lambda c: c LIKE '%_id' OR c = 'amount') FROM sales_db.sales_parquet WHERE order_id = 1
The query returns the following result.
| order_id | amount |
|---|---|
|
|
The following SQL query passes the customer and region columns to concat_ws as separate arguments, so that concat_ws joins them with a hyphen.
SELECT concat_ws( '-', *COLUMNS(['customer', 'region']) ) AS label FROM sales_db.sales_parquet WHERE order_id = 1
The query returns the following result.
| label |
|---|
|
Considerations
Write a lambda selector with the lambda keyword, as in COLUMNS(lambda c: c LIKE '%_id'). DuckDB does not accept the arrow form, c -> condition.
A column named in EXCLUDE or REPLACE must exist in the FROM clause. RENAME ignores a column name that does not exist, so check the names that you rename.