Skip to main content
SQL • LESSON 154

Previous Salary

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

Advanced3 Minutes1670 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