From the employees table, retrieve the employee name, department, and their salary rank within the department (highest salary rank 1).
๐ ๐ฒ: Challenge accepted!
SELECT
name,
department,
salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
I applied
DENSE_RANK() window function partitioned by department and ordered by descending salary to assign ranks within each department. Unlike ROW_NUMBER(), DENSE_RANK() handles ties by assigning the same rank without gaps. This is ideal for leaderboards or performance analytics.๐ง๐ถ๐ฝ ๐ณ๐ผ๐ฟ ๐ฆ๐ค๐ ๐๐ผ๐ฏ ๐ฆ๐ฒ๐ฒ๐ธ๐ฒ๐ฟ๐:
Master window function differences (ROW_NUMBER vs RANK vs DENSE_RANK)โthey're interview staples for deduping, paging, and top-N queries!
React with โค๏ธ for more