Complete the code to select the top 1 salary per department.
SELECT department, employee, salary FROM employees WHERE salary = (SELECT MAX(salary) FROM employees WHERE department = [1]);The subquery filters salaries by the same department as the outer query, so we use department to match groups.
Complete the code to assign row numbers partitioned by department ordered by salary descending.
SELECT department, employee, salary, ROW_NUMBER() OVER (PARTITION BY [1] ORDER BY salary DESC) AS rn FROM employees;We partition by department to restart row numbers for each department group.
Fix the error in the query to select top 3 salaries per department using ROW_NUMBER.
SELECT department, employee, salary FROM (SELECT department, employee, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY [1] DESC) AS rn FROM employees) sub WHERE rn <= 3;
We order by salary descending to rank employees by highest salary per department.
Fill both blanks to select the top 2 employees by salary per department using DENSE_RANK.
SELECT department, employee, salary FROM (SELECT department, employee, salary, DENSE_RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS rank FROM employees) sub WHERE rank <= 2;
Partition by department to group employees, and order by salary descending to rank top salaries.
Fill all three blanks to select the top 1 salary per department using a correlated subquery.
SELECT department, employee, salary FROM employees e1 WHERE salary = (SELECT MAX([1]) FROM employees e2 WHERE e2.[2] = e1.[3]);
The subquery finds the max salary for the same department as the outer query.