Jan has a good article about finding the row matching some value in the group:
This is one illustration of group by limitations in SQL language
which is not offset by any MySQL specific extensions,yet
As you can see if you want to get one row from the group which is
sorted some way you have to use ugly hacks. This is because SQL
does not have a notion of sorting the data within the groups and
in fact ANSI SQL even forbids you to select columns which are not
aggregates or part of group by because result in this case is not
defined.
What would be quite helpful is to have GROUPORDER (pick the name) clause which defines which element is selected for non aggregate columns, something like
PLAIN TEXT SQL:
- SELECT MAX(Population), City, Country FROM City GROUP BY Country GROUPORDER …