Select earliest hired employee from employee table

dato datuashvili picture dato datuashvili · Nov 24, 2011 · Viewed 29.4k times · Source

I have an employee table where the fields are:

first_name, last_name, hire_date, salary, department_id, department_name, and so on.

I am asked to find most earliest hired employees. I know functions like max, sysdate, and months_between, but I don't know how to use it here?

I was thinking that this problem could be solved like this:

select e.first_name,max(e.hire_date)
from employees e

but it generates the following error:

 oRA-00937: not a single-group group function
    00937. 00000 -  "not a single-group group function"
    *Cause:    
    *Action:
    Error at Line: 1 Column: 7

So please give me hint what will be correct form?

Answer

p.campbell picture p.campbell · Nov 24, 2011

To find the earliest hired employee:

 SELECT * FROM
 (
     SELECT * 
     FROM  employees
     ORDER BY hire_date
 )
 WHERE rownum = 1;

If you want the top 10 earliest hired employees:

 SELECT * FROM 
 (
   SELECT * 
   FROM  employees
   ORDER BY hire_date
 )
 WHERE rownum <= 10;

This is using Oracle's pseudocolumn ROWNUM