how to find nth highest salary

Answer Posted / suprotim agarwal

CREATE TABLE #Employees (EmpID int, EmpName varchar(10),
Salary int)
INSERT #Employees SELECT 1,'Tim',345345
INSERT #Employees SELECT 2,'Jill',76845
INSERT #Employees SELECT 3,'Kathy',234545
INSERT #Employees SELECT 4,'Jack',74564
INSERT #Employees SELECT 5,'Harry',56756456
INSERT #Employees SELECT 6,'Arnol',75675
INSERT #Employees SELECT 7,'Dave',76566
INSERT #Employees SELECT 8,'Zinnade',58776
INSERT #Employees SELECT 9,'Iflar',345567
INSERT #Employees SELECT 10,'Jimmy',76766

Highest Salary Of an Employee

SELECT EmpName, Salary
from
(
SELECT EmpName, Salary, Row_Number() OVER(ORDER BY SALARY
DESC) AS 'Salaries'
FROM #Employees
) emp
WHERE Salaries = 1

2nd highest salary


SELECT EmpName, Salary
from
(
SELECT EmpName, Salary, Row_Number() OVER(ORDER BY SALARY
DESC) AS 'Salaries'
FROM #Employees
) emp
WHERE Salaries = 2

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What to check if a User database is locked?

667


Can a table be moved to different filegroup?

651


What is the xml datatype?

718


Explain difference between cross join and full outer join?

654


What is difference between foreign key and unique key?

653






Define master database?

658


What is molap and its advantage? : sql server analysis services, ssas

621


How to attach adventureworkslt physical files to the server?

720


What are data driven subscriptions?

85


How to add an address record into adventureworkslt?

660


When is update_statistics command used?

675


What is the difference between lock, block and deadlock? : sql server database administration

668


What types of replication are supported in sql server?

655


What are the types of ssrs?

115


What area unit the various kinds of info compression introduced in sql server 2008?

657