second highest and avg salary
How to find second highest salary and avg salary
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajeesh MenothPosted Dec 16, 2015, 2:56 AM
An explanation of the solution
The SQL above first finds the highest salary value in the Employee table using “(select MAX(Salary) from Employee)”. Then, adding the “WHERE Salary NOT IN” in front basically creates a new set of Salary values that does not include the highest Salary value. For instance, if the highest salary in the Employee table is 200,000 then that value will be excluded from the results using the “NOT IN” operator, and all values except for 200,000 will be retained in the results.
This now means that the highest value in this new result set will actually be the 2nd highest value in the Employee table. So, we then select the max Salary from the new result set, and that gives us 2nd highest Salary in the Employee table. And that is how the query above works.
Ravi PatelPosted Dec 16, 2015, 2:46 AM
Supreeth JPosted Dec 16, 2015, 1:59 AM
Venkata SubbareddyPosted Dec 16, 2015, 1:35 AM
Venkata SubbareddyPosted Dec 16, 2015, 1:28 AM