mannerpots
mannerpots

Reputation: 175

How can I select the row(s) which contains the maximum value in a column?

I have a table as follows:

|name|extra|value|
|a   |m    |1    |
|b   |n    |2    |
|c   |o    |3    |
|d   |p    |4    |

I want to select (d, p) from it.

If I add a row so my table is now:

|name|extra|value|
|a   |m    |1    |
|b   |n    |2    |
|c   |o    |3    |
|d   |p    |4    |
|e   |q    |4    |

Then I want to get two rows back from my table, (d,p) and (e, q). How can I achieve this in Postgres?

Upvotes: 0

Views: 107

Answers (1)

Hambone
Hambone

Reputation: 16377

You should be able to use the max of values as an argument in the where clause:

select name, extra
from table
where value = (select max (value) from table)

Upvotes: 1

Related Questions