Intern
What window functions have you used most often? How does RANK differ from DENSE_RANK?
sobes.tech AI
Answer from AI
Window functions are most commonly used in analytics for ranking, aggregation, and calculating moving metrics.
-
RANK()assigns a rank to rows within a window, giving the same rank to identical values, and creating gaps in ranks if there are duplicates. For example, if two rows share rank 1, the next will be rank 3. -
DENSE_RANK()is similar toRANK(), but ranks are consecutive without gaps. If two rows have rank 1, the next will be rank 2.
Example:
| Value | RANK() | DENSE_RANK() |
|---|---|---|
| 100 | 1 | 1 |
| 100 | 1 | 1 |
| 90 | 3 | 2 |
| 80 | 4 | 3 |
Thus, DENSE_RANK is useful when continuous ranking is important, while RANK accounts for gaps due to duplicates.