Skip to main content
SQL โ€ข LESSON 154

Previous Salary

How can we view the immediately lower salary in each department?

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

How can we view the immediately lower salary in each department?

๐Ÿ’ก WHAT IS IT?

LAG() within a partition retrieves the previous row's value scoped strictly to the current department.

๐ŸŽฏ WHAT IS IT USED FOR?

Pay tier gap analysis, step-function compensation reviews, local step comparison.

๐Ÿ’ป EXAMPLE
SELECT
department_id,
name,
salary,
LAG(salary) OVER (
    PARTITION BY department_id
    ORDER BY salary
) AS previous_salary
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Previous Salary.

  • โœ“ Partitioned LAG
  • โœ“ Department lookback
  • โœ“ Step comparison
  • โœ“ Compensation ladder