Skip to content
Advertisement

Stuck to select maximum row

I have a table with columns:

I need to select row with maximum value, and show info like

I tried using group function MAX(), but I can’t select fullname, because if I use it as a GROUP BY expression, it will select max in groups.

Other way, is to use WITH statement, order table by value desc, use

function and maximum value will be on max_id = 1, and then use

to remove other rows.

But I think there is a way to do this better and I can’t find one.

UPDATE:

A tricky solution to this problem is

Advertisement

Answer

The simplest way is to sort the data and pull one row:

If you want ties, you can add with ties to the fetch first.

Another method is:

This can have very good performance with an index on value.

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