title: Getting the MAX Record for the Most Recent Serial Numbers
body: <p>I have the following table with some sample data.</p>
  
  <pre><code>Record_ID    Counter    Serial    Owner
  1           0           AAA       Jack
  2           1           AAA       Kevin
  3           0           BBB       Jane
  4           1           BBB       Wendy
  </code></pre>
  
  <p>Based on data similar to the above, I am trying to write a SQL query for MySQL that gets the record with the maximum <code>Counter</code> value per <code>Serial</code> number.  <b>The part I seem to be having trouble with is getting the query to get the last 50 unique serial numbers that were updated.</b></p>
  
  <p>Below is the query I came up with so far based on <a href="https://stackoverflow.com/questions/6860746/sql-selecting-multiple-columns-based-on-max-value-in-one-column">this StackOverflow question</a>.</p>
  
  <pre><code>SELECT * 
  FROM   `history` his 
     INNER JOIN(SELECT serial, 
                       Max(counter) AS MaxCount 
                FROM   `tracking` 
                WHERE  serial IN (SELECT serial 
                                  FROM   `history`) 
                GROUP  BY serial
                ORDER  BY record_id DESC) q 
             ON his.serial = q.serial 
                AND his.counter = q.maxcount 
  LIMIT  0, 50
  </code></pre>