Skip to main content
SQL • LESSON 149

Salary Difference

How can we calculate how much an employee earns above or below the overall company average?

Advanced3 Minutes1620 XP
🤔 THE QUESTION

How can we calculate how much an employee earns above or below the overall company average?

💡 WHAT IS IT?

Subtracting global window aggregates (AVG() OVER ()) from row values computes instant variances.

🎯 WHAT IS IT USED FOR?

Compensation variance reports, SLA latency deviations, anomaly scoring.

💻 EXAMPLE
SELECT
name,
salary,
salary - AVG(salary) OVER () AS difference_from_average
FROM employees;

🎯 Mission Objectives

Practice typing production-grade SQL code for Salary Difference.

  • Variance calculation
  • Global window average
  • Deviation analysis
  • Mathematical expressions on windows