Search code examples
mysqlsqlmax

Selecting all people with the max age?


I want to get values from a row with the largest certain value (in this example, the oldest member)

Select * from members where age=max(age)

Will this work?

And what will happen if there is more than 1 oldest members with the same age?

(I don't have to worry about it because I use auto_increment, but I just got curious)

Thanks.


Solution

  • select * 
    from members 
    where age = (select max(age) as max_age from members);
    

    If there are more than 1 member with the same maximum age, you will get multiple results. To select just one from that:

    select * 
    from members 
    where age = (select max(age) as max_age from members);
    limit 1
    

    You can optionally add an order by if you favor any particular data over a random one.