title: Fetch the row which has the Max value for a column in SQL Server
body: <p>I found a question that was very similar to this one, but using features that seem exclusive to Oracle. I'm looking to do this in SQL Server.</p>
  
  <p>I have a table like this:</p>
  
  <pre><code>MyTable
  --------------------
  MyTableID  INT  PK
  UserID     INT
  Counter    INT
  </code></pre>
  
  <p>Each user can have multiple rows, with different values for <code>Counter</code> in each row. I need to find the rows with the highest <code>Counter</code> value for each user.</p>
  
  <p>How can I do this in SQL Server 2005? </p>
  
  <p>The best I can come up with is a query the returns the <code>MAX(Counter)</code> for each <code>UserID</code>, but I need the entire row because of other data in this table not shown in my table definition for simplicity's sake.</p>
  
  <p><strong>EDIT:</strong> It has come to my attention from some of the answers in this post, that I forgot an important detail. It is possible to have 2+ rows where a UserID can have the same MAX counter value. Example below updated for what the expected data/output should be.</p>
  
  <p>With this data:</p>
  
  <pre><code>MyTableID   UserID   Counter
  ---------   -------  --------
  1           1         4
  2           1         7
  3           4         3
  4           11        9
  5           11        3
  6           4         6
  ...
  9           11        9
  </code></pre>
  
  <p>I want these results for the duplicate MAX values, select the first occurance in whatever order SQL server selects them. Which rows are returned isn't important in this case as long as the UserID/Counter pairs are distinct:</p>
  
  <pre><code>MyTableID   UserID    Counter
  ---------   -------   --------
  2           1         7
  4           11        9
  6           4         6
  </code></pre>