Expressions and operators

PRQL

SQL

  1. WITH table_1 AS (
  2. SELECT
  3. diameter * 3.14159 AS circumference,
  4. color
  5. FROM
  6. foo
  7. )
  8. SELECT
  9. color
  10. FROM
  11. table_1 AS table_0
  12. WHERE
  13. circumference > 10
  14. AND color <> 'red'

This table shows operator precedence. Use parentheses () to prioritize operations and for function calls (see the discussion below.)

PRQL uses parentheses () for several purposes:

  • Parentheses group operands to control the order of evaluation, for example: ((1 + x) * y)

  • Parentheses delimit a minus sign of a function argument, for example: add (-1) (-3)

  • Parentheses delimit nested function calls that contain a pipe, either the | symbol or a new line. “Nested” means within a transform; i.e. not just the main pipeline, for example: (column-name | in 0..20)

  • Parentheses wrap a function call that is part of a larger expression on the right-hand side of an assignment, for example: round 0 (sum distance)

Here’s a set of examples of these rules:

  1. SELECT
  2. distance BETWEEN 0 AND 20 AS is_proximate,
  3. SUM(distance) OVER () AS total_distance,
  4. distance / 40 AS travel_time,
  5. ROUND(distance, 2) AS distance_rounded_2_dp,
  6. distance >= 100 AS is_far,
  7. distance BETWEEN -100 AND 0,
  8. distance BETWEEN -100 AND 0 AS is_negative,
  9. AVG(distance) OVER () AS average_distance
  10. FROM
  11. employees
  12. ORDER BY
  13. distance DESC

Note: The total_distance statement below generates an error because the function is not in a list. (The PRQL compiler should display a better error message.)

  1. Error:
  2. ╭─[:2:29]
  3. 2 derive total_distance = sum distance # generates the error shown below
  4. ────┬───

Note