Clone and init the database (requires a local PostgreSQL instance):

  1. cargo run compile examples/employees/average-title-salary.prql | psql -U postgres -d employees
  • for each employee, find their average salary,
  • join employees with their departments and titles (duplicating employees for each of their titles and departments)
  • group by department and title, aggregating average salary
  • join with department to get department name

PRQL

  1. from salaries
  2. group [emp_no] (
  3. aggregate [emp_salary = average salary]
  4. )
  5. join t=titles [==emp_no]
  6. join dept_emp side:left [==emp_no]
  7. group [dept_emp.dept_no, t.title] (
  8. aggregate [avg_salary = average emp_salary]
  9. )
  10. join departments [==dept_no]
  11. select [dept_name, title, avg_salary]

SQL

  1. from e=employees
  2. join salaries [==emp_no]
  3. group [e.emp_no, e.gender] (
  4. aggregate [
  5. emp_salary = average salaries.salary
  6. ]
  7. )
  8. join de=dept_emp [==emp_no] side:left
  9. group [de.dept_no, gender] (
  10. aggregate [
  11. salary_avg = average emp_salary,
  12. ]
  13. )
  14. join departments [==dept_no]
  15. select [dept_name, gender, salary_avg, salary_sd]

  1. WITH table_3 AS (
  2. SELECT
  3. e.gender,
  4. AVG(salaries.salary) AS _expr_0,
  5. e.emp_no
  6. FROM
  7. JOIN salaries ON e.emp_no = salaries.emp_no
  8. GROUP BY
  9. e.emp_no,
  10. e.gender
  11. ),
  12. table_1 AS (
  13. SELECT
  14. table_2.gender,
  15. AVG(table_2._expr_0) AS salary_avg,
  16. STDDEV(table_2._expr_0) AS salary_sd,
  17. de.dept_no
  18. FROM
  19. table_3 AS table_2
  20. LEFT JOIN dept_emp AS de ON table_2.emp_no = de.emp_no
  21. GROUP BY
  22. de.dept_no,
  23. table_2.gender
  24. )
  25. SELECT
  26. departments.dept_name,
  27. table_0.gender,
  28. table_0.salary_avg,
  29. table_0.salary_sd
  30. FROM
  31. table_1 AS table_0
  32. JOIN departments ON table_0.dept_no = departments.dept_no

PRQL

SQL

  1. WITH table_3 AS (
  2. SELECT
  3. e.gender,
  4. e.emp_no
  5. FROM
  6. employees AS e
  7. JOIN salaries ON e.emp_no = salaries.emp_no
  8. e.emp_no,
  9. e.gender
  10. ),
  11. table_1 AS (
  12. SELECT
  13. AVG(table_2._expr_0) AS salary_avg,
  14. STDDEV(table_2._expr_0) AS salary_sd,
  15. dm.emp_no
  16. FROM
  17. table_3 AS table_2
  18. JOIN dept_emp AS de ON table_2.emp_no = de.emp_no
  19. JOIN dept_manager AS dm ON dm.dept_no = de.dept_no
  20. AND (de.from_date, de.to_date) OVERLAPS (dm.from_date, dm.to_date)
  21. GROUP BY
  22. dm.emp_no,
  23. table_2.gender
  24. )
  25. SELECT
  26. managers.first_name || ' ' || managers.last_name AS mng_name,
  27. managers.gender,
  28. table_0.salary_avg,
  29. table_0.salary_sd
  30. FROM
  31. table_1 AS table_0
  32. JOIN employees AS managers ON table_0.emp_no = managers.emp_no

  1. from de=dept_emp
  2. join s=salaries side:left [
  3. (s.emp_no == de.emp_no),
  4. s"({s.from_date}, {s.to_date}) OVERLAPS ({de.from_date}, {de.to_date})"
  5. ]
  6. group [de.emp_no, de.dept_no] (
  7. aggregate salary = (average s.salary)
  8. )
  9. join employees [==emp_no]