Complete the code to select the first salary in each department using FIRST_VALUE.
SELECT department_id, FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary DESC) AS first_salary FROM employees WHERE salary > [1];The query filters employees with salary greater than 3000 and uses FIRST_VALUE to get the highest salary per department.
Complete the code to select the last hire date in each department using LAST_VALUE.
SELECT department_id, LAST_VALUE(hire_date) OVER (PARTITION BY department_id ORDER BY hire_date ROWS BETWEEN UNBOUNDED PRECEDING AND [1]) AS last_hire FROM employees;LAST_VALUE requires the window frame to extend to UNBOUNDED FOLLOWING to return the last value in the partition correctly.
Fix the error in the query to correctly get the first salary per department.
SELECT department_id, FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY [1]) AS first_salary FROM employees;Ordering by salary ASC returns the smallest salary as the first value, which is the usual intent for FIRST_VALUE.
Fill both blanks to get the last hire date per department correctly.
SELECT department_id, LAST_VALUE(hire_date) OVER (PARTITION BY department_id ORDER BY hire_date ROWS BETWEEN [1] AND [2]) AS last_hire FROM employees;
The window frame must start from UNBOUNDED PRECEDING and end at UNBOUNDED FOLLOWING for LAST_VALUE to work as expected.
Fill all three blanks to select the first and last salary per department and filter salaries above 4000.
SELECT department_id, FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary [1]) AS first_salary, LAST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary ROWS BETWEEN [2] AND [3]) AS last_salary FROM employees WHERE salary > 4000;
Ordering salary ascending gets the lowest salary first. The window frame from UNBOUNDED PRECEDING to UNBOUNDED FOLLOWING ensures LAST_VALUE returns the correct last salary.