SQL, SQL Server, Tutorials, Oracle, PL/SQL, Interview Questions & Answers, Joins, Multiple Choice Questions, Quiz, Stored Procedures, Select, Insert, Update, Delete and other latest topics on SQL, SQL Server and Oracle.
SQL - Finding Second Highest Salary
How to find the second highest salary?
EMPLOYEE table has fields EMP_ID and SALARY how do you find the second highest salary?
Solution:
We can write a sub-query to achieve the result
SELECT MAX(SALARY)
FROM EMPLOYEE
WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM EMPLOYEE)
The first sub-query [SELECT MAX(SALARY) FROM EMPLOYEE] in the WHERE clause will return the MAX SALARY in the table and the main query SELECT’s the MAX SALARY from the results which doesn’t have the highest SALARY.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment