Calculate Employee Salary Difference From Average

Find the difference between an employee's salary and the department's average salary.

sql

SELECT id, salary, salary - AVG(salary) OVER (PARTITION BY department_id) AS salary_difference FROM employees;

This query uses a window function to calculate the average salary per department and subtracts that value from each individual employee's salary to determine the difference.

PromptDB can make mistakes. Please double-check responses.