SELECT list
The select list names the columns and expressions that the query returns, separated by commas. To name an output column, follow its expression with AS alias. To return every column of the FROM clause, use *.
An expression later in the select list can refer to the alias of an earlier one. The WHERE, GROUP BY, and HAVING clauses can also refer to select list aliases.
#1 refers to the first column of the FROM clause, #2 to the second, and so on. A comma after the last item of the select list is allowed.
Syntax
SELECT [ ALL | DISTINCT ] expression [ [ AS ] alias ]
[, ...]
SELECT #n [ [ AS ] alias ] [, ...]
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 and row of the sales_parquet table, sorted by order_id.
SELECT * FROM sales_db.sales_parquet ORDER BY order_id
The query returns the following results.
| order_id | customer | region | amount | order_date |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following SQL query names an expression doubled and uses that alias in the next expression of the same select list.
SELECT order_id, amount * 2 AS doubled, doubled + 1 AS doubled_plus_one FROM sales_db.sales_parquet WHERE order_id <= 2 ORDER BY order_id
The query returns the following results.
| order_id | doubled | doubled_plus_one |
|---|---|---|
|
|
|
|
|
|
The following SQL query uses #2 to select the second column of a VALUES list.
SELECT #2 AS second_column FROM (VALUES (1, 'a'), (2, 'b')) AS t(id, name) ORDER BY second_column
The query returns the following results.
| second_column |
|---|
|
|