Search code examples
sqloracle-databasedatetimegreatest-n-per-group

how to compare two datetime in sql oracle


I need to show the names of the oldest employee and of the last one so i need to compare datetime of the hire_date, here's how i tried and its not working:

SELECT ename
from emp
where hiredate = 'MIN(hiredate)' AND hiredate = 'MAX(hiredate)'; 

what should i do?


Solution

  • For example:

    SQL> with minimax as
      2    (select min(hiredate) min_hiredate,
      3            max(hiredate) max_hiredate
      4     from emp
      5    )
      6  select e.ename, e.hiredate
      7  from emp e join minimax m on e.hiredate in (m.min_hiredate, m.max_hiredate);
    
    ENAME      HIREDATE
    ---------- ----------
    SMITH      17.12.1980
    ADAMS      12.01.1983
    
    SQL>