Search code examples
mysqlsqlmaxmin

How to select the row with the minimum value of a column while the other column is at its maximum?


In SQL, how can I select the minimum value of a column a (integer) where the column b (integer) is the maximum value for that column, using only one select ?

SELECT MIN(a) OVER (PARTITION BY MAX(b)) as res #seems to be ill-formed
FROM table

For instance

a b
1 1
1 3
2 3

should return

res
1

Solution

  • order by and limit should be good enough here:

    select a
    from mytable
    order by b desc, a
    limit 1