Skip to main content
SQL โ€ข LESSON 155

Salary Change

How can we calculate the exact monetary difference between consecutive employee salaries?

Advancedโฑ 3 Minutesโญ 1680 XP
๐Ÿค” THE QUESTION

How can we calculate the exact monetary difference between consecutive employee salaries?

๐Ÿ’ก WHAT IS IT?

Subtracting LAG(salary) from the current salary measures the incremental step between sorted records.

๐ŸŽฏ WHAT IS IT USED FOR?

Band gap analysis, pay step distribution, detecting compensation compression.

๐Ÿ’ป EXAMPLE
SELECT
name,
salary,
salary - LAG(salary) OVER (
    ORDER BY salary
) AS salary_difference
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Salary Change.

  • โœ“ Delta from previous
  • โœ“ Salary difference
  • โœ“ Incremental steps
  • โœ“ Analytical math on LAG