Search code examples
sqlsql-serverdatetimemax

How can I calculate the oldest age for a male person given their date of birth in SQL?


How to calculate maximum age of a male person from a list containing date of birth in sql?

I tried

Select name, datediff(yy, dateofbirth, getdate()) as age
From table 
Where gender = male 

But I don’t know how to use the max function to get the max age


Solution

  • Do something like this to get the least recent date of birth.

    SELECT name, datediff(yy, dateofbirth, getdate()) as age, dateofbirth
      FROM tbl
     WHERE gender = 'm'
     ORDER BY dateofbirth
     LIMIT 1
    

    If you use Microsoft SQL Server, you do this.

    SELECT TOP (1)
           name, datediff(yy, dateofbirth, getdate()) as age, dateofbirth
      FROM tbl
     WHERE gender = 'm'
     ORDER BY dateofbirth