How do I: remove duplicates?

PRQL doesn’t have a specific distinct keyword. Instead duplicate tuples in a relation can be removed by using group and take 1:

PRQL

from db.employees
select department
group employees.* (
  take 1
)

SQL

SELECT
  DISTINCT department
FROM
  db.employees

This also works with a wildcard:

PRQL

from db.employees
group employees.* (take 1)

SQL

SELECT
  DISTINCT *
FROM
  db.employees

Remove duplicates from each group?

To select a single row from each group group can be combined with sort and take:

PRQL

# youngest employee from each department
from db.employees
group department (
  sort age
  take 1
)

SQL

WITH table_0 AS (
  SELECT
    *,
    ROW_NUMBER() OVER (
      PARTITION BY department
      ORDER BY
        age
    ) AS _expr_0
  FROM
    db.employees
)
SELECT
  *
FROM
  table_0
WHERE
  _expr_0 <= 1

Note that we can’t always compile to DISTINCT; when the columns in the group aren’t all the available columns, we need to use a window function:

PRQL

from db.employees
group {first_name, last_name} (take 1)

SQL

WITH table_0 AS (
  SELECT
    *,
    ROW_NUMBER() OVER (PARTITION BY first_name, last_name) AS _expr_0
  FROM
    db.employees
)
SELECT
  *
FROM
  table_0
WHERE
  _expr_0 <= 1