Pages

Sunday, April 15, 2007

Youngest Employee - SQL TOP

********************Youngest & Oldest Employee********************

To get the youngest / olderst employee, two different methods are shown here.

1. Using Max and Min Functions
2. Using Top in Select Statement

'' Youngest Employee (Reference : AdventureWorks)


Select Min(BirthDate) from HumanResources.Employee
Select TOP 1 BirthDate from HumanResources.Employee Order By 1 Asc
Select TOP 1 * from HumanResources.Employee Order By BirthDate Asc

Select * from Person.Contact where ContactID = (Select TOP 1 EmployeeID from HumanResources.Employee Order By BirthDate Asc)

'' Oldest Employee (Reference : AdventureWorks)


Select Max(BirthDate) from HumanResources.Employee
Select TOP 1 BirthDate from HumanResources.Employee Order By 1 Desc







Free Search Engine Submission


Free Search Engine Submission



SQL Tips & Tricks


AddMe - Search Engine Optimization


Homerweb Search

2 comments:

  1. i tired with my table agents

    select top 1 birth_date from agents
    order by birth_date asc
    this does not work this gives error
    select top 1 birth_date from agents
    *

    ERROR at line 1:
    ORA-00923: FROM keyword not found where expected




    i am trying to extract young member and the name of the member.
    tired


    select a.agent_id,a.first_name,a.birth_date,l.description

    from agents a, locations l

    where
    a.location_id=l.location_id
    and
    rownum <=1

    order by a.birth_date desc

    this one does not give the youngest... the birthdate is somewhere in mid like 32 and the youngest is in year 1990
    please tell me the solution

    ReplyDelete
  2. try this...
    select p.Nativeplace, p.Name, p.Dobirth from personal p
    having p.Dobirth=(select max(Dobirth) from personal p2 where p.Nativeplace=p2.Nativeplace group by p2.Nativeplace);

    ReplyDelete