Write a query to get 2nd maximum salary in an employee table ?

Answer Posted / basheer

ex:
Raj 200
kamal 300
hajka 500
Suresh 200
so 1st max salary is 500,2nd is 300,3rd is 200
we need 2nd maximum only(i.e 300)
Query is below

SELECT MIN(SALARY) FROM EMPLOYEE WHERE SALARY IN (SELECT
DISTINCT TOP 2 SALARY FROM EMPLOYEE ORDER BY SALARY DESC)

I've checked this query.
it will give 2nd maximum value.
if it is 3rd max salary then use TOP 3 instead of TOP 2
if u need detail explanation:
1)
Qry: SELECT DISTINCT TOP 2 SALARY FROM EMPLOYEE ORDER BY
SALARY DESC
Output:500
300
2)
SELECT MIN(SALARY) FROM EMPLOYEE WHERE SALARY IN (SELECT
DISTINCT TOP 2 SALARY FROM EMPLOYEE ORDER BY SALARY DESC)
ans: it gets minimum salary from subquery( from above 1st ans)

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can instead of triggers be used to fire once for each statement on a view?

543


What are secondary keys?

563


what is text? : Sql dba

564


How to prepare for oracle pl sql certification?

577


What is schema in sql example?

601






What is a design view?

525


discuss about myisam index statistics collection. : Sql dba

531


what are the 'mysql' command line options? : Sql dba

561


what are the differences between public, private, protected, static, transient, final and volatile? : Sql dba

560


What is asqueryable?

535


How does a trigger work?

541


what happens if you no create privilege in a database? : Sql dba

536


What is a natural join sql?

515


Can a commit statement be executed as part of a trigger?

605


What does <> sql mean?

543