Skip to content
Advertisement

Calculates the difference between employee salary and the average salary of job group

Given a table like:

How to write a query that for each employee calculates the difference between his/her salary to the average salary of his/her job group?

To get the avg. Salary for each group I use:

But I’m struggling to find the difference between salary and avg. salary for each job title.

Advertisement

Answer

Depending on which database you are using, you may be able to arrive at this more efficiently using either window functions or CTEs. But a SQL solution that should work on pretty much any SQL-based database you could use would look like:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement