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?

Advancedโฑ 3 Minutesโญ 1620 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