How can we view the immediately lower salary in each department?
LAG() within a partition retrieves the previous row's value scoped strictly to the current department.
Pay tier gap analysis, step-function compensation reviews, local step comparison.
SELECT
department_id,
name,
salary,
LAG(salary) OVER (
PARTITION BY department_id
ORDER BY salary
) AS previous_salary
FROM employees;Practice typing production-grade SQL code for Previous Salary.