Complete the code to select the 3rd salary value in the ordered salary list for each department.
SELECT department, salary, NTH_VALUE(salary, [1]) OVER (PARTITION BY department ORDER BY salary) AS third_salary FROM employees;The NTH_VALUE function returns the value of the salary at the specified position in the ordered set. Here, 3 means the 3rd salary.
Complete the code to get the 1st salary value in the ordered salary list for each department.
SELECT department, salary, NTH_VALUE(salary, [1]) OVER (PARTITION BY department ORDER BY salary DESC) AS highest_salary FROM employees;Using 1 as the second argument returns the first value in the ordered list, which is the highest salary because of DESC order.
Fix the error in the code to correctly get the 2nd salary value per department.
SELECT department, salary, NTH_VALUE(salary, [1]) OVER (PARTITION BY department ORDER BY salary) AS second_salary FROM employees;The second argument must be 2 to get the 2nd value. Using 0 or 1 is incorrect.
Fill both blanks to get the 4th highest salary per department.
SELECT department, salary, NTH_VALUE(salary, [1]) OVER (PARTITION BY department ORDER BY salary [2]) AS fourth_highest FROM employees;
To get the 4th highest salary, use 4 as the position and order salaries descending (DESC).
Fill all three blanks to get the 2nd lowest salary per department and alias it as second_lowest_salary.
SELECT department, salary, NTH_VALUE(salary, [1]) OVER (PARTITION BY department ORDER BY salary [2] ROWS BETWEEN UNBOUNDED PRECEDING AND [3]) AS second_lowest_salary FROM employees;
To get the 2nd lowest salary, use position 2, order ascending (ASC), and frame rows between unbounded preceding and current row.