Skip to content
Advertisement

Get row with highest or lowest value from a GROUP BY

I’m trying to get the row with the highest/lowest number, after performing a GROUP BY:

Here is my test data

To get the lowest value, I’ll use MIN()

Now, the id row2 is 2, but it should be 4.

I also tried with a join:

How can I get the correct ID for each result based on what the lowest value is?

Advertisement

Answer

I think this is what you are trying to achieve:

Output:

ID VALUE NAME
1 10 row1
4 5 row2

See this SQLFiddle

Here I have self-joined the table with minVal and Name.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement