Retrieve Previous Employee Salary Using LAG

Find the previous employee's salary using a window function.

sql

SELECT id, name, salary, LAG(salary) OVER (ORDER BY id) AS previous_salary FROM employees;

This query uses the LAG() window function to retrieve the salary value from the preceding row, ordered by the employee id, and displays it alongside the current salary.

PromptDB can make mistakes. Please double-check responses.