Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading