What is a Window Function, and what is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?
Last updated
A window function computes a value over related rows via
OVER (PARTITION BY ... ORDER BY ...)without collapsing rows likeGROUP BY.ROW_NUMBER()gives unique sequential numbers with ties broken arbitrarily (1, 2, 3, 4).RANK()gives ties the same rank then skips (1, 2, 2, 4).DENSE_RANK()gives ties the same rank with no gaps (1, 2, 2, 3).
- https://www.freecodecamp.org/news/window-functions-in-sql/
- https://www.freecodecamp.org/news/how-to-optimize-pyspark-jobs-handbook/
- https://learn.microsoft.com/en-us/sql/t-sql/functions/ranking-functions-transact-sql?view=sql-server-ver17
- https://dev.to/sharefun2023/rownumber-vs-rank-vs-denserank-one-of-them-silently-duplicates-your-top-n-i6a
- https://www.freecodecamp.org/news/sql-concepts-for-data-analysts/
- https://learn.microsoft.com/is-is/azure/Databricks/sql/language-manual/functions/row_number
- https://dev.to/anwaar/sql-rownumber-rank-and-denserank-7ik