title: Calculate mode in SQL
body: <p>I have seen the answer from a previous post, which works fine but I have a small dilemma.
  Taking the same scenario:</p>
  
  <p><br>A table that list students' grades per class. I want a result set that looks like: </p>
  
  <pre><code>BIO...B 
  CHEM...C 
  </code></pre>
  
  <p><br>Where the "B" and "C" are the modes for the class and want to get the mode for the class.</p>
  
  <p>Once I applied the below query, i got the following output:</p>
  
  <pre><code>Class | Score | Freq  |  Ranking 
  2010  |   B   | 8     |    1 
  2010  |   C   | 8     |    1 
  2011  |   A   | 10    |    1 
  2012  |   B   | 11    |    1
  </code></pre>
  
  <p>In 2010, I have two grades with the same frequency. What if..I just want to display the highest score, in this case will be "B". How can I achieve that? I would need to assign rankings to the letter grades, but I'm not sure how. Please advise.
  Thanks.</p>
  
  <p>Prior post:
  <a href="https://stackoverflow.com/questions/2045075/sql-server-mode-sql">SQL Server mode SQL</a></p>
  
  <p>The query I used to retrieve the data was the answer from Peter:</p>
  
  <pre><code>;WITH Ranked AS ( 
  SELECT 
      ClassName, Grade 
      , GradeFreq = COUNT(*) 
      , Ranking = DENSE_RANK() OVER (PARTITION BY ClassName ORDER BY COUNT(*) DESC) 
  FROM Scores 
  GROUP BY ClassName, Grade 
  ) 
  SELECT * FROM Ranked WHERE Ranking = 1
  </code></pre>