SQL interview question: Salary vs Department Average

Compare Employee Salary to Department Average

Each employee's review includes how their salary compares to their department's average.

Return employee_name (full name), dept_name, salary, dept_avg_salary (rounded to 0 decimals), and diff_from_avg (salary minus dept average, rounded to 0). Order by diff_from_avg descending. Limit to 10.

Skills: window functions with AVG() OVER (PARTITION BY)

Tables

employees 37 rows
  • id integer PK
  • first_name text
  • last_name text
  • email text
  • department_id integer → departments
  • salary integer
  • hire_date text
  • manager_id integer → employees
  • title text
idfirst_namelast_nameemaildepartment_idsalaryhire_datemanager_idtitle
1AliceChen[email protected]12500002015-01-15NULLCEO
2RobertMartinez[email protected]21800002016-03-20NULLCEO
3SarahJohnson[email protected]11500002017-06-101VP Engineering
departments 5 rows
  • id integer PK
  • name text
  • location text
idnamelocation
1EngineeringSan Francisco
2SalesNew York
3MarketingLos Angeles

Keep going