Hi......
I am working in ADO.NET and face a problem in query. How can we find out the second highest salary from any table and also define about where clause?
Loading
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
Pravin MoreRank #1,419 · 1.2K reputationPosted Jan 10, 2012, 6:12 PM EST
use query like below..........
select distinct top 1 salary from Employee where salary not in (select top 1 salary from Employee)
Thanks,
Pravin.
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 10, 2012, 6:15 PM EST
Select max(salary) from employee where salary NOT IN
(Select max(salary) from employee)
Or
select distinct(salary) from employee A where 2=(select count(distinct(salary)) from employee B where A.salary<=B.salary)
Thanks
Mukesh KumarRank #615 · 3.9K reputationPosted Jan 10, 2012, 6:18 PM EST
Here is an example to find out nth highest salary from Employee table
SELECT TOP 1 salaryFROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
where n > 1 (n is always greater than one)
Shen Hengbin94 reputationPosted Jan 10, 2012, 6:21 PM EST
try my sample . u can copy it in your query browser directly.
create table #test (name varchar(10),salary int)
insert into #test select 'a',80
insert into #test select 'b',65
insert into #test select 'c',80
insert into #test select 'd',75
insert into #test select 'e',95
insert into #test select 'f',88
insert into #test select 'g',90 -- this is the second highest
select
*
from
(select name , salary ,rank()over(order by salary desc) n from #test)t
where
n=2
drop table #test
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 10, 2012, 8:45 PM EST
try this.
select * from (
select top 2 employeeID,salary,ROW_NUMBER()OVER(ORDER BY salary DESC) as RowNum
from employee) A
where A.RowNum = 2
hope this help.