GeekInterview.com
Series: Subject: Topic:
Question: 9 of 214

How to retrieve a second highest salary from a table?

How to retrieve a second highest salary from a table? Note:Suppose salaries are in duplicate values eg: Name Sal Malli 60000 Pandi 60000 Rudra 45000 Ravi 45000
Asked by: BJMallinatha | Member Since Jun-2012 | Asked on: Jun 21st, 2012

View all questions by BJMallinatha

Showing Answers 1 - 4 of 4 Answers
anup

Answered On : Jun 22nd, 2012

you can specify a sql query in source qualifier like
Select name,sal from Employee order by sal---it is used to sort the salary column so that the max salary will be in the starting
still we have one more issue
If two or more persons have the highest salary then the second row wont be the second highest salary so inorder to get the 2nd highest salary
-- Take an expression transformation
-- pass the sal as input port--I_sal (input + output port)
-- create a variable port-- v_sal which has the value of I_sal

-- Create another variable port v_logic which has the expression as IIF(v_sal=v_prevsal,NO,secondHighsal)
which returns No if the current sal and previous salary are equal if not it will return secondHighsal
--create one more output port -- v_result as string and give the value of v_logic to it
--create a variable port v_prevsal
make sure that this port should be created after the variable port in which we gave the expression
because it should retain the previous value when it is being used.

-- Next create a filter transformation and pass the I_sal and v_Result ports to that and check the V_result for the
seconHighSal value and pass the respective sal to target

Hope you will get the solution

  
Login to rate this answer.
PSUNIL

Answered On : Aug 7th, 2012

USING DENSE RANK U CAN GET IT

Code
  1. SELECT SALARY FROM
  2. (SELECT SALARY ,DENSE_RANK() OVER(ORDER BY SALARY) AS DENSE_RANK FROM EMP)
  3. WHERE DENSE_RANK=2;

  
Login to rate this answer.
purvi

Answered On : Aug 7th, 2012

select max(salary) from EmpTable where salary not in (select max(salary) from EmpTable));

  
Login to rate this answer.
NagaRaja

Answered On : Aug 17th, 2012

Select Max(Salary) from Emp Where Salary < (Select Max(Salary) from Emp)

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.