Complete the code to assign a unique row number to each employee ordered by salary.
SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY [1]) AS row_num FROM employees;The ROW_NUMBER() function assigns a unique number to each row ordered by the specified column. Here, ordering by salary numbers employees from lowest to highest salary.
Complete the code to rank employees by salary, allowing ties to have the same rank.
SELECT employee_id, salary, RANK() OVER (ORDER BY [1] DESC) AS salary_rank FROM employees;The RANK() function assigns ranks to rows ordered by salary descending. Employees with the same salary get the same rank, and gaps appear in ranks after ties.
Complete the code to assign dense ranks to employees by salary within each department.
SELECT employee_id, department, salary, DENSE_RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dept_salary_rank FROM employees;
The PARTITION BY department clause groups rows by department. The DENSE_RANK() ORDER BY salary DESC ranks salaries within each department with no gaps after ties.
Fill both blanks to rank employees by salary within each department using RANK(), ordering salaries descending.
SELECT employee_id, department, salary, RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dept_rank FROM employees;
The PARTITION BY groups employees by department. The ORDER BY orders salaries descending within each department for ranking.
Fill all three blanks to assign dense ranks to employees by department and salary, and filter to show only top 3 ranks per department.
WITH ranked_employees AS (SELECT employee_id, department, salary, DENSE_RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dense_rank FROM employees) SELECT * FROM ranked_employees WHERE dense_rank [3] 3;
Partition by department to group employees. Order by salary descending to rank salaries. Filter ranks less than or equal to 3 to get top 3 per department.