How to find second highest or maximumNilofers Shaikh9yAsked Oct 31, 2017, 8:23 PM EDTSolvedShareLinkedInXCopy linkSaveHow to find second highest or second maximum value from table.
How to find second highest or maximumNilofers Shaikh9yAsked Oct 31, 2017, 8:23 PM EDTSolvedShareLinkedInXCopy linkSaveHow to find second highest or second maximum value from table.
PBPoonam Bhosale25 reputation9y agoPosted Oct 31, 2017, 8:47 PM EDT✓ Accepted answer+1select max(column_name) from table_name where column_name < (select max(column_name) from table_name) For example :-select max(Salary) from Employeewhere Salary < (select max(Salary ) from Employee)+1Nilesh PatilRank #1,129 · 1.6K reputation9y agoPosted Oct 31, 2017, 8:49 PM EDT0Hi, This query find secon highest sallary of employee select * from Employee where Salary In (select MAX(Salary) from Employee where Salary NOT IN (Select MAX(Salary) from employee)); 0Amit KumarRank #459 · 5.4K reputation9y agoPosted Oct 31, 2017, 9:11 PM EDT0hi, select top(1) id from ( select top (2) id from Tab1 order by id desc) as oo order by id asc 0Vighnesh Gadekar56 reputation9y agoPosted Oct 31, 2017, 9:20 PM EDT0Hi Nilofers, lets consider a Employee table with empid & salary empid | Salary=====|=======1 |1002 |2003 |300 if you fire this below querySELECT TOP 1 Salary AS SecHightestSalary FROM ( SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) AS TempTable ORDER BY Salary ASC; you will get output, SecHightestSalary ============200 screenshot: Hope this will help you.0Ravi PatelRank #439 · 5.7K reputation9y agoPosted Oct 31, 2017, 9:23 PM EDT0Hi, SELECT TOP 1 salary FROM ( SELECT DISTINCT TOP 2 salary FROM employee ORDER BY salary DESC) a ORDER BY salary 0AAAmit Arora110 reputation9y agoPosted Oct 31, 2017, 10:15 PM EDT0;with temp as(Select ROW_NUMBER() Over(order by Salary ASC) as RowNum, Salary from Employee)Select Salary from temp where RowNum = 20Vighnesh Gadekar56 reputation9y agoPosted Oct 31, 2017, 10:32 PM EDT0Hi Amit Arora, Your query is perfect just change ACS with DESC as mentioned below,;with temp as(Select ROW_NUMBER() Over(order by Salary DESC) as RowNum, Salary from Employee)Select Salary from temp where RowNum = 20Tushar DikshitRank #1,378 · 1.2K reputation9y agoPosted Oct 31, 2017, 10:48 PM EDT0Hi, You can refer following two options for specific second highest or Nth highets. N could be second, third etc. Find the second highest salary.Select max(Salary) from employee where Salary not in (Select max(Salary) from employee) Now, can you find 3rd, 5th or 6th i.e. N'th highest Salary?Query for 3rd highest salarySELECT * FROM EMPLOYEE E WHERE 2 = (SELECT COUNT(DISTINCT E1.SALARY) FROM EMPLOYEE E1 WHERE E1.SALARY>E.SALARY) Here, 2= 3-1 i.e. N-1 ; can be applied for any number. For more details you can visit C# corner article - http://www.c-sharpcorner.com/article/most-asked-sql-queries-in-interview-questions/0Nilofers Shaikh42 reputation9y agoPosted Oct 31, 2017, 10:56 PM EDT0thank u... :)0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nilesh PatilRank #1,129 · 1.6K reputation9y agoPosted Oct 31, 2017, 8:49 PM EDT0Hi, This query find secon highest sallary of employee select * from Employee where Salary In (select MAX(Salary) from Employee where Salary NOT IN (Select MAX(Salary) from employee)); 0
Amit KumarRank #459 · 5.4K reputation9y agoPosted Oct 31, 2017, 9:11 PM EDT0hi, select top(1) id from ( select top (2) id from Tab1 order by id desc) as oo order by id asc 0
Vighnesh Gadekar56 reputation9y agoPosted Oct 31, 2017, 9:20 PM EDT0Hi Nilofers, lets consider a Employee table with empid & salary empid | Salary=====|=======1 |1002 |2003 |300 if you fire this below querySELECT TOP 1 Salary AS SecHightestSalary FROM ( SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) AS TempTable ORDER BY Salary ASC; you will get output, SecHightestSalary ============200 screenshot: Hope this will help you.0
Ravi PatelRank #439 · 5.7K reputation9y agoPosted Oct 31, 2017, 9:23 PM EDT0Hi, SELECT TOP 1 salary FROM ( SELECT DISTINCT TOP 2 salary FROM employee ORDER BY salary DESC) a ORDER BY salary 0
AAAmit Arora110 reputation9y agoPosted Oct 31, 2017, 10:15 PM EDT0;with temp as(Select ROW_NUMBER() Over(order by Salary ASC) as RowNum, Salary from Employee)Select Salary from temp where RowNum = 20
Vighnesh Gadekar56 reputation9y agoPosted Oct 31, 2017, 10:32 PM EDT0Hi Amit Arora, Your query is perfect just change ACS with DESC as mentioned below,;with temp as(Select ROW_NUMBER() Over(order by Salary DESC) as RowNum, Salary from Employee)Select Salary from temp where RowNum = 20Tushar DikshitRank #1,378 · 1.2K reputation9y agoPosted Oct 31, 2017, 10:48 PM EDT0Hi, You can refer following two options for specific second highest or Nth highets. N could be second, third etc. Find the second highest salary.Select max(Salary) from employee where Salary not in (Select max(Salary) from employee) Now, can you find 3rd, 5th or 6th i.e. N'th highest Salary?Query for 3rd highest salarySELECT * FROM EMPLOYEE E WHERE 2 = (SELECT COUNT(DISTINCT E1.SALARY) FROM EMPLOYEE E1 WHERE E1.SALARY>E.SALARY) Here, 2= 3-1 i.e. N-1 ; can be applied for any number. For more details you can visit C# corner article - http://www.c-sharpcorner.com/article/most-asked-sql-queries-in-interview-questions/0Nilofers Shaikh42 reputation9y agoPosted Oct 31, 2017, 10:56 PM EDT0thank u... :)0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tushar DikshitRank #1,378 · 1.2K reputation9y agoPosted Oct 31, 2017, 10:48 PM EDT0Hi, You can refer following two options for specific second highest or Nth highets. N could be second, third etc. Find the second highest salary.Select max(Salary) from employee where Salary not in (Select max(Salary) from employee) Now, can you find 3rd, 5th or 6th i.e. N'th highest Salary?Query for 3rd highest salarySELECT * FROM EMPLOYEE E WHERE 2 = (SELECT COUNT(DISTINCT E1.SALARY) FROM EMPLOYEE E1 WHERE E1.SALARY>E.SALARY) Here, 2= 3-1 i.e. N-1 ; can be applied for any number. For more details you can visit C# corner article - http://www.c-sharpcorner.com/article/most-asked-sql-queries-in-interview-questions/0
Poonam Bhosale25 reputationPosted Oct 31, 2017, 8:47 PM EDT
select max(column_name) from table_name
select max(Salary) from Employee
Nilesh PatilRank #1,129 · 1.6K reputationPosted Oct 31, 2017, 8:49 PM EDT
Amit KumarRank #459 · 5.4K reputationPosted Oct 31, 2017, 9:11 PM EDT
Vighnesh Gadekar56 reputationPosted Oct 31, 2017, 9:20 PM EDT
Ravi PatelRank #439 · 5.7K reputationPosted Oct 31, 2017, 9:23 PM EDT
Amit Arora110 reputationPosted Oct 31, 2017, 10:15 PM EDT
Vighnesh Gadekar56 reputationPosted Oct 31, 2017, 10:32 PM EDT
(
Select ROW_NUMBER() Over(order by Salary DESC) as RowNum, Salary from Employee
)
Select Salary from temp where RowNum = 2
Tushar DikshitRank #1,378 · 1.2K reputationPosted Oct 31, 2017, 10:48 PM EDT
Nilofers Shaikh42 reputationPosted Oct 31, 2017, 10:56 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts