Question: I have a table like this:

```
| Key | Value | Message |
|-----|-------|---------|
| a   | 1     | xx      |
| a   | 2     | yy      |
| b   | 5     | mm      |
| b   | 4     | nn      |

```

I would like to group the data by Key, get the minimum Value of each group, and the related Message.
The result is expected to be:

```
| Key | Value | Message |
|-----|-------|---------|
| a   | 1     | xx      |
| b   | 4     | nn      |

```

I'm using MySQL 5.7. Is it possible to do that?

Answer: 
You can use `row_number()` :

```
select t.*
from (select t.*, row_number() over (partition by key order by value) as seq
      from table t
     ) t
where seq = 1;

```

You can also use correlated `subquery` if ranking function not support :

```
select t.*
from table t
where t.value = (select min(t1.value) from table t1 where t1.key = t.key);

```
First create a subquery that returns `min value` for each `key`, then join with the main table:

```
select t.* 
from tablename t
inner join (
   select key, min(value) min_value
   from tablename
   group by key
) i on i.key = t.key and i.min_value = t.value

```