title: selecting top N rows for each group in a table
body: <p>I am facing a very common issue regarding "Selecting top N rows for each group in a table".</p>
  
  <p>Consider a table with <code>id, name, hair_colour, score</code> columns.</p>
  
  <p>I want a resultset such that, for each hair colour, get me top 3 scorer names.</p>
  
  <p>To solve this i got exactly what i need on <a href="http://rickosborne.org/blog/index.php/2008/01/07/sql-getting-top-n-rows-for-a-grouped-query/" rel="noreferrer">Rick Osborne's blogpost "sql-getting-top-n-rows-for-a-grouped-query"</a></p>
  
  <p>That solution doesn't work as expected when my scores are equal.</p>
  
  <p>In above example the result as follow.</p>
  
  <pre><code> id  name  hair  score  ranknum
  ---------------------------------
   12  Kit    Blonde  10  1
    9  Becca  Blonde  9  2
    8  Katie  Blonde  8  3
    3  Sarah  Brunette 10  1    
    4  Deborah Brunette 9  2 - ------- - - &gt; if
    1  Kim  Brunette 8  3
  </code></pre>
  
  <p>Consider the row <code>4 Deborah Brunette 9 2</code>. If this also has same score (10) same as Sarah, then ranknum will be 2,2,3 for "Brunette" type of hair.</p>
  
  <p>What's the solution to this?</p>