Monday 13 July 2015

SQL Query To Get Top 2 Highest Salary Employee Details In a SQL Server Table

In the previous class we are discussing about “SQL query to get highest salary employee details”. One more important Technical interview question is “How to write SQL Query To Get Top 2 Highest Employee Details In A SQL Server Table”.


                If you have a table in SQL Server with name of “employee” and column names of “eno, ename, salary, dno, managerID. The employee table is as following.

employee-table-sql-server

If you are writing a query To Get Top 2 Highest Salary Employee Details as follows

Select top 2 salary from employee

then the Output is as follows.
sql-server-top-2

You can clearly observe that the output is incorrect for Top 2 Highest Salary Employee Details In a SQL Server Table. Because if you are not given order by query then top 2 query by default takes least values from employee table.

So now you can write query by using Order by as follows

Select top 2 salary from employee order by salary desc

Now you can see the output, it matches to your question - SQL Query To Get Top 2 Highest Salary Employee Details In A SQL Server Table?.

But this is also not a full answer, because it shows only top2 salary. It will not show employee details. So to get employee details who have top 2 highest salary, write query as

               Select * from employee where salary in (
                                             Select top 2 salary from employee order by salary desc
                                                                                     )

Now you can see the output in the following table

Top-2-Highest-Salary-Employee-Details-SQL-Server-Table

So here you can get the exact answer for “How to write SQL Query To Get Top 2 Highest Employee Details In A SQL Server Table”.


Conclusion: If you any Queries related to “How to write SQL Query To Get Top 2 Highest Employee Details In A SQL Server Table”.  Then comment here, we are ready to help you.

You May also search in Google as:
Freshers Junction – SQL Query for top 2 highest salary employee details, SQL query to two highest salary employee details, top 2 employee salary details in SQL Server, SQL Server Query to get Top 2 Highest Employee Details In A SQL Server Table. Top 2 values in SQL Server. 

0 comments:

 
close

Join Telegram Channel

Join FreshersJobsUpdates - Your Job Search End Here

Join Telegram Channel