Saturday, July 23, 2011

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

3 comments:

Ante said...

can u get oldest and youngest with 1 select? and how

Unknown said...

Try this to display youngest employee from each native place:

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);

Mahesh Guttedar Kalgi said...

OLDEST EMployee

select FirstName as [oldest] from Employee where dob in (
select min(dob) from Employee )

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Deep's | Bloggerized by Deep - Deep's Templates | ElearSQL-Server