user1041338
user1041338

Reputation:

taking max element(s)

I have the following table :

     X     Y                  X --> Y   and X is a primary key 
    __________
    1     2323
    2     3122
    3     4343
    4     4343
    5     123

I want print out X according to max Y value .

output :

     X     Y
    __________
     3    4343
     4    4343

How can I do that ?

Upvotes: 1

Views: 81

Answers (2)

xQbert
xQbert

Reputation: 35343

Select X,Y from table where Y=(Select max(y) from table)

Edited (User wanted both X & Y in output)

Upvotes: 3

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

Typically it'd be simple:

SELECT MAX(`Y`) FROM `table`

It's a little more complex since you want to account for duplicate Y values and pull out all relevant rows:

SELECT * FROM `table`
WHERE `Y` = (SELECT MAX(`Y`) FROM `table`)

Hopefully this is fairly self-explanatory.

Upvotes: 0

Related Questions