title: mysql select top n max values
body: <p>How can you select the top n max values from a table?</p>
  
  <p>For a table like this:</p>
  
  <pre><code>column1  column2
     1       foo
     2       foo
     3       foo
     4       foo
     5       bar
     6       bar
     7       bar
     8       bar
  </code></pre>
  
  <p>For n=2, the result needs to be:</p>
  
  <pre><code>3    
  4    
  7    
  8    
  </code></pre>
  
  <p>The approach below selects only the max value for each group.</p>
  
  <pre><code>SELECT max(column1) FROM table GROUP BY column2
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>4
  8
  </code></pre>