Maybe I'm just too used to non-standard extensions of our database but the SQL example could, at least for our db, be rewritten as
SELECT TOP 20
title,
country,
AVG(salary) AS average_salary,
SUM(salary) AS sum_salary,
AVG(gross_salary) AS average_gross_salary,
SUM(gross_salary) AS sum_gross_salary,
AVG(gross_cost) AS average_gross_cost,
SUM(gross_cost) AS sum_gross_cost,
COUNT(*) as count
FROM (
SELECT
title,
country,
salary,
(salary + payroll_tax) AS gross_salary,
(salary + payroll_tax + healthcare_cost) AS gross_cost
FROM employees
WHERE country = 'USA'
) emp
WHERE gross_cost > 0
GROUP BY title, country
ORDER BY sum_gross_cost
HAVING count > 200
This cuts down the repetition a lot, and can also help the optimizer in certain cases. Could do another nesting to get rid of the HAVING if needed.Still, think the PRQL looks very nice, especially with a "let" keyword as mentioned in another thread here.