how to find nth highest salary
Answers were Sorted based on User's Feedback
Answer / saravanan
The below 2 queries will work
n - The nth highest salary
First query
------------------------
SELECT MAX(salary)
FROM
(SELECT salary
FROM emp
WHERE salary IS NOT NULL
ORDER BY salary DESC)
WHERE rownum < &n;
Second query
------------------------
SELECT DISTINCT(a.salary)
FROM emp a
WHERE &n =
(SELECT COUNT(DISTINCT(b.salary))
FROM emp b
WHERE a.salary <= b.salary)
;
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / parmesh m
select top 1 salary from Employee where salary in (
select top n salary from Employee
group by salary
having count(salary )< n+1
order by salary desc )
order by salary
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kalai
SELECT * FROM EMP A WHERE 7=(SELECT COUNT(SAL) FROM EMP
WHERE A.SAL<SAL)
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sanwar hudda
n th Highest salary
select salary from test t1 where n-1=(select COUNT (*) from
test
where salary > t1.salary )
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sikindar
select * from emp where sal=(
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal));
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / jaishree singh
select emp_name,salary from
(select emp_name,salary, row_number() over (order by salary)
rownumber from employee) E
where E.rownumber = 3
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / neeraj kumar
select top(1) Salary
from(select distinct top(n) Salary
from emp
order by Salary desc ) a
order by Salary
Note-you can find out any position salary through top(n).
n=1,2,3,4,5,6,7,8,9..................
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / eldho k scaria
There are two methods:(Suppose table name is EMPLOYEE,
Colomn name is SALARY, and N=6)
SELECT * FROM EMPLOYEE
WHERE SALARY IN
(SELECT TO 1 SALARY (SELECT DISTINCT TOP 6 SALARY FROM
EMPLOYEE ORDER BY SALARY DESC)
A ORDER BY SALARY)
or
SELECT * FROM EMPLOYEE E1
WHERE (6-1)=(SELECT COUNT (DISTINCT (E2.SALARY))
FROM EMPLOYEE E2
WHERE E2.SALARY>E1.SALARY)
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / anija
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC)
ORDER BY salary
| Is This Answer Correct ? | 1 Yes | 0 No |
select salary from tablename t1
where n=(select count(*)from tablename t2
where t1.salary<=t2.salary)
| Is This Answer Correct ? | 16 Yes | 16 No |
What are the differences between ms sql server & oracle?
Where do we generally create INDEX?
1 Answers Ernst Young, Thomson Reuters,
How to change a login name in ms sql server?
Can Somebody tell me the difference between Clustered & Non- Clustered Index??
two tables with table name ship(name,year) and battle (name,year),how to find the latest added year in the table ship
Differentiate between a local and a global temporary table?
How to write character string constants or literals in ms sql server?
what is unique and xaml nonclustered index
is there more the two primary key in a single table?
26 Answers Eforce, Systematix,
What are the properties and different types of sub-queries?
What is store procedure?
when we use function instead of procedure? plz tell me the situation with ex?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)