Selecting First/Least/Max Row per Group in SQL
July 24, 2012
Here are some common SQL problems, all of which have related solutions:
How do I find the most recent log entry for each program? How do I find the most popular item from each category? How do I find the top score for each player? In general, these types of “select the extreme from each group” queries can be solved with the same techniques. I’ll explain how to do that in this article, including the harder problem of selecting the top N entries, not just the top 1.