locked
lowest and highest salaries display RRS feed

  • Question

  • User639567535 posted

    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



    and this is the output




    this is the queries which i try

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

    select top 5 salary from Employees;

    Monday, January 25, 2016 3:50 AM

Answers

  • User177399542 posted

    You need this:

    select top 5 salary from Employees order by salary DESC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 4:35 AM
  • User1724605321 posted

    Hi ,

    Below statements are for your reference:

    To get the top 5 rows order by some field:

    SELECT TOP 5[ID] FROM table ORDER BY <YourColumn> ASC/DESC

    To get only 2nd row:

    WITH Resulttable AS 
    ( 
    SELECT TOP 5 
        *, ROW_NUMBER() OVER(ORDER BY YourColumn) AS RowNumber 
        FROM @Table 
    )  
    SELECT * FROM Resultstable WHERE RowNumber=2

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 6:00 AM

All replies

  • User177399542 posted

    You need this:

    select top 5 salary from Employees order by salary DESC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 4:35 AM
  • User1724605321 posted

    Hi ,

    Below statements are for your reference:

    To get the top 5 rows order by some field:

    SELECT TOP 5[ID] FROM table ORDER BY <YourColumn> ASC/DESC

    To get only 2nd row:

    WITH Resulttable AS 
    ( 
    SELECT TOP 5 
        *, ROW_NUMBER() OVER(ORDER BY YourColumn) AS RowNumber 
        FROM @Table 
    )  
    SELECT * FROM Resultstable WHERE RowNumber=2

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 6:00 AM
  • User-698989805 posted

    The following should return the second highest salary:

    SELECT TOP 5 Salary AS 'Second Highest Salary' FROM (SELECT DISTINCT TOP 2 Salary FROM SalaryTable ORDER BY Salary DESC) a ORDER BY Salary ASC

    Tuesday, January 26, 2016 10:20 AM
  • User-62323503 posted

    Refer below post to find nth highest salary 

    http://www.itdeveloperzone.com/2012/01/find-nth-highest-salary-in-sql.html

    Wednesday, January 27, 2016 8:08 AM