Search code examples
mysqlsqlsql-order-byminsql-limit

Select Min or Max record from a table along with the corresponding name field


Assuming a table Family with Name and Age and records

Bob 55
Alice 40
Marky 12

If I run

Select Name,Min(Age) from Family 

I get

Bob,12

I'm trying to ask for the fields from the single record with the lowest age yet I'm simply getting the first record Name and the Age from the record with the lowest value.

How can I use Min() to make that request?


Solution

  • I understand that you want the entire row that has the smallest age. Assuming that you don't care about ties, you can just sort the rows by ascending age and retain the first record only:

    select *
    from family
    order by age
    limit 1