Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i try to get top 5 salary and second highest salary from employees table but it shows me also lowest salaries

check this picture ssalry column
salary column[^]


and this is the output

output[^]


this is the queries which i try

SQL
SELECT MAX(Salary) FROM Employees
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employees )

select top 5 salary from Employees;
Posted
Updated 24-Jan-16 17:00pm
v3

Please try this:

To get nth highest salary:

SQL
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employees
ORDER BY salary DESC) a
ORDER BY salary


in above, replace n to 2 & you will get 2nd highest salary as you required.

And for top 5 salary list please try :

SQL
SELECT DISTINCT TOP 5 salary
FROM employees
ORDER BY salary DESC



For more help please see the link below & customize the query as per your need:

SQL SERVER – Find Nth Highest Salary of Employee – Query to Retrieve the Nth Maximum value – Journey to SQL Authority with Pinal Dave[^]
 
Share this answer
 
v2
I am not really sure if I understand what you want here but I will try and attempt. Are you looking for top 5 salaries from the table? If yes, doesn't this fit?

SQL
select distinct top 5 salary from employees order by salary desc
 
Share this answer
 
v2
Comments
super_user 24-Jan-16 23:08pm    
yes i am looking for top 5 salaries .. but when i do this this shows me also lowest salaries like 2500 which is lowest salary and i want top 5 highest salaries
dan!sh 24-Jan-16 23:17pm    
How many distinct salary values do you have in table?
super_user 24-Jan-16 23:12pm    
???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900