SELECT t1.*
FROM yourtable t1
LEFT OUTER JOIN yourtable t2
ON (t1.name = t2.name AND t1.seqnum > t2.seqnum)
WHERE t2.seqnum IS NULL;
Re. Dems' comment:
If you simply use GROUP BY name
then your select-list can only include name
and the aggregate function MIN()
. It's often the case that you really want the whole row where the minimum value occurs per group.
If you use the TOP
solution (or LIMIT
if you use MySQL, PostgreSQL, SQLite), then you can't get the minimum for multiple groups based on name
, you can only get the minimum for one name
.
I read between the lines of the OP's question and guessed that they want the whole row, that there are more columns than those shown in the question (there always are), and that the desired query should return results for multiple names, not just one.
Re. SquareCog's comment:
The solution you suggest is also a join:
SELECT t1.*
FROM yourtable t1
JOIN (SELECT name, MIN(seqnum) AS seqnum FROM yourtable GROUP BY name) t2
USING (name, seqnum);
However, this solution probably can't use indexes to evaluate the join, whereas the solution I gave can.