title: Give priority to ORDER BY over a GROUP BY in MySQL without subquery
body: <p>I have the following query which does what I want, but I suspect it is possible to do this without a subquery:</p>
  
  <pre><code>  SELECT * 
      FROM (SELECT * 
              FROM 'versions' 
          ORDER BY 'ID' DESC) AS X 
  GROUP BY 'program'
  </code></pre>
  
  <p>What I need is to group by program, but returning the results for the objects in versions with the highest value of "ID".</p>
  
  <p>In my past experience, a query like this <em>should</em> work in MySQL, but for some reason, it's not:</p>
  
  <pre><code>  SELECT * 
      FROM 'versions' 
  GROUP BY 'program' 
  ORDER BY MAX('ID') DESC
  </code></pre>
  
  <p>What I <strong>want</strong> to do is have MySQL do the ORDER BY first and <em>then</em> the GROUP BY, but it insists on doing the GROUP BY first followed by the ORDER BY. i.e. it is sorting the results of the grouping instead of grouping the results of the ordering.</p>
  
  <p>Of course it is not possible to write</p>
  
  <p><code>SELECT * FROM 'versions' ORDER BY 'ID' DESC GROUP BY 'program'</code></p>
  
  <p>Thanks.</p>