Wednesday, January 18, 2006

Top Intv Ques: Nth Highest Salary of Emp !!

I m always having trouble to build this query but now i got it forever!!!

Say "Emp" table structure is:

EmpID Salary
1 100
2 500
3 200
4 400

Say we have to find 2nd highly paid employee...Here we go.....::

select Salary from Emp A where 1 = (select count(Salary) from Employee B where B.Salary > A.Salary)

Note: "1" in above query is Nth Max - 1 or in other words if we have to find say 3rd highest see how many are there above three ?? Obviously two sp put 2 na intead of 1....(here we are looking for 2nd highest thats why i have put 1...Samje...)


Simple and Perfect....Kya baat hai!!! Wah Wah....

No comments: