Oracle Sql (Age In Years Of Youngest Employee)

I have a table called "employees" and I I need to get the age of the youngest employee in years.
For example if I look at the table the youngest employee is "57 years old"

the columns:

EmployeeID, Lastname, Title, Birthdate,
Hiredate, City, Country

The code I was trying was this:

SELECT MAX(birthdate)FROM employees;

With that I can get the date of birth of the youngest employee, but now I need to somehow compare it with the current date that would be using "sysdate" and then change it to numbers so that it shows that he is 57 years old, but I have not succeeded

>Solution :

If you don’t care much about months and days, a simple option is to extract year from sysdate and youngest birthdate and subtract them:

Sample data:

SQL> with employees (employeeid, lastname, birthdate) as
  2    (select 1, 'Little', date '2015-08-25' from dual union all  --> youngest
  3     select 2, 'Foot'  , date '2000-11-13' from dual
  4    )

Query:

  5  select extract(year from sysdate) - extract(year from max(birthdate)) as age
  6  from employees;

       AGE
----------
         8

SQL>

Leave a Reply