๐จโ๐ป Me: Here's a quick example using salaries:
SELECT name, department, salary,
RANK() OVER (ORDER BY salary DESC) AS rank_salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank_salary
FROM employees;
โ Key Differences:
โ RANK(): Skips ranks if there's a tie (e.g., two at #1, next is #3)โgreat for competitions where gaps show true position.
โ DENSE_RANK(): No gapsโranks increase sequentially (e.g., two at #1, next is #2)โideal for leaderboards or tiers without skips.
๐ Example:
If two people tie at 1st place:
โฆ RANK() โ 1, 1, 3
โฆ DENSE_RANK() โ 1, 1, 2
๐ก Use DENSE_RANK() when you want consistent rank steps, like in sales reportsโadd PARTITION BY department for per-group ranking!
๐ฌ Tap โค๏ธ for more!