Calculate Salary Difference Using LAG Function

Find the difference between an employee's salary and the previous employee's salary.

sql

SELECT id, salary, salary - LAG(salary) OVER (ORDER BY id) AS salary_difference FROM employees;

This query uses the LAG() window function to access the salary of the preceding row based on the id column and calculates the difference from the current row's salary.

PromptDB can make mistakes. Please double-check responses.