Select
Picks and computes columns.
select [
{name} = {expression},
# or
{column},
]
# or
select ![{column}]
Examples
PRQL
from employees
select name = f"{first_name} {last_name}"
SQL
SELECT
CONCAT(first_name, ' ', last_name) AS name
FROM
employees
PRQL
from employees
select [
name = f"{first_name} {last_name}",
age_eoy = dob - @2022-12-31,
]
SQL
SELECT
CONCAT(first_name, ' ', last_name) AS name,
dob - DATE '2022-12-31' AS age_eoy
FROM
employees
PRQL
from employees
select first_name
SQL
SELECT
first_name
FROM
employees
PRQL
from e=employees
select [e.first_name, e.last_name]
SQL
SELECT
first_name,
last_name
FROM
employees AS e
Excluding columns
We can use !
to exclude a list of columns. This can operate in two ways:
- We use
SELECT * EXCLUDE
/SELECT * EXCEPT
for the columns supplied toselect ![]
in dialects which support it. - Otherwise, the columns must have been defined prior in the query (unless all of a table’s columns are excluded); for example in another
select
or agroup
transform. In this case, we evaluate and specify the columns that should be included in the output SQL.
Some examples:
PRQL
prql target:sql.bigquery
from tracks
select ![milliseconds,bytes]
SQL
SELECT
*
EXCEPT
(milliseconds, bytes)
FROM
tracks
PRQL
from tracks
select [track_id, title, composer, bytes]
select ![title, composer]
SQL
SELECT
track_id,
bytes
FROM
tracks
PRQL
from artists
derive nick = name
select ![artists.*]
SQL
SELECT
name AS nick
FROM
artists
Note
In the final example above, the e
representing the table / namespace is no longer available after the select
statement. For example, this would raise an error:
from e=employees
select e.first_name
filter e.first_name == "Fred" # Can't find `e.first_name`
To refer to the e.first_name
column in subsequent transforms, either refer to it using first_name
, or if it requires a different name, assign one in the select
statement:
PRQL
from e=employees
select fname = e.first_name
filter fname == "Fred"
SQL
WITH table_1 AS (
SELECT
first_name AS fname
FROM
employees AS e
)
SELECT
fname
FROM
table_1 AS table_0
WHERE
fname = 'Fred'
`