Complete the code to get the previous salary of each employee using LAG.
SELECT employee_id, salary, LAG(salary) OVER (ORDER BY employee_id) AS previous_salary FROM employees ORDER BY employee_id [1];The LAG function looks at the previous row based on the order. We order by employee_id ascending to get the previous salary correctly.
Complete the code to get the next salary of each employee using LEAD.
SELECT employee_id, salary, LEAD(salary) OVER (ORDER BY employee_id) AS next_salary FROM employees ORDER BY employee_id [1];The LEAD function looks at the next row based on the order. We order by employee_id ascending to get the next salary correctly.
Fix the error in the code to correctly get the previous and next salaries.
SELECT employee_id, salary, LAG(salary) OVER (ORDER BY [1]) AS prev_sal, LEAD(salary) OVER (ORDER BY employee_id) AS next_sal FROM employees;Both LAG and LEAD should use the same ordering column to compare rows properly. Here, employee_id is the correct column to order by.
Fill both blanks to calculate the salary difference between current and previous employee.
SELECT employee_id, salary, salary - [1](salary) OVER (ORDER BY employee_id) AS salary_diff FROM employees ORDER BY [2] employee_id;
Use LAG to get the previous salary and subtract it from current salary. Order by employee_id ASC to get correct sequence.
Fill all three blanks to find the percentage change in salary compared to the previous employee.
SELECT employee_id, salary, ROUND(((salary - [1](salary) OVER (ORDER BY employee_id)) * 100.0) / [2](salary) OVER (ORDER BY employee_id), 2) AS pct_change FROM employees ORDER BY [3] employee_id;
Use LAG to get the previous salary for both subtraction and division. Order by employee_id ASC for correct row sequence. This calculates the percentage change from previous salary.