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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Venkata SubbareddyRank #2,083 · 691 reputationPosted Dec 15, 2015, 8:28 PM EST
Venkata SubbareddyRank #2,083 · 691 reputationPosted Dec 15, 2015, 8:35 PM EST
SELECT TOP 1 column FROM YOUR_TABLE ORDER BY column DESCSupreeth JRank #2,205 · 639 reputationPosted Dec 15, 2015, 8:59 PM EST
Ravi PatelRank #439 · 5.7K reputationPosted Dec 15, 2015, 9:46 PM EST
Rajeesh MenothRank #118 · 18.4K reputationPosted Dec 15, 2015, 9:56 PM EST
SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )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.