Explore Library
Code Quiz

Percent-of-total With a Window Frame Bug

A running total sneaks in when an ORDER BY silently changes the default window frame.

Codesql
SELECT
  employee_id,
  department_id,
  salary,
  salary * 1.0 / SUM(salary) OVER (
    PARTITION BY department_id
    ORDER BY salary
  ) AS pct_of_dept_total
FROM employees;

This query should return each employee's salary as a fraction of their department's TOTAL salary, but the numbers are wrong. What is the bug?