Skip to content

Instantly share code, notes, and snippets.

@EvanCarroll
Created February 3, 2010 18:21
Show Gist options
  • Save EvanCarroll/293849 to your computer and use it in GitHub Desktop.
Save EvanCarroll/293849 to your computer and use it in GitHub Desktop.
If three runners join a race and the first two runners tie, is the third runner to cross the finish line in second place or in third? This is the difference between `rank()`, and `dense_rank()`. Both of these functions agree that the first and second runners are in a tie for first place, but `rank()` will assign the third runner to the third place. In this configuration you would say two runners tied for first place and the third runner came in third place. Conversely, dense_rank() will say the first two runners tied for first place, and the third runner came in second place. In SQL parlance, the `dense_rank()` has no *gaps* you can not skip a second place because two of the runners preceding it were tied. In comparison, `rank()` will have gaps anytime two consecutive rows preceding it tied by the **ORDER BY** clause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment