Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to get the first two employees name from a table having highest salary in department "IT"?

user-image
Question added by Deleted user
Date Posted: 2014/10/03
Prospero Sevilla
by Prospero Sevilla , IT Generalist , Abu Zaid & El Essawi Group

Using SQL query, you can try the very simple solution below:

 

SELECT TOP2 EmployeeName, Salary

FROM EmployeeFile

WHERE Department='IT'

ORDER BY Salary DESC

MOHAMED MAHMOUD EISSA
by MOHAMED MAHMOUD EISSA , Software department MGR. and Senior developer , Global Media Services GMS

Asume for example we  have identical top5 salaries in IT department.

I think the most easy way would be something like :

select top2 empName from empTable

where department='IT'

order by sal desc , empName  asc

as it will give you the top2 salaries sorted by name in case the top salaries are identical

In my openion in this case it would be better to know the5 top salaries as getting only2 reslts of5 identical results has not much meaning.

 

But i think

select top2 empName from empTable

where department='IT'

order by sal desc

is enough

We can add the salary in the select statment if it is required as the questaion states "How to get the first two employees name"

Muhammad Waqas
by Muhammad Waqas , C#/.Net Developer , Signup solution

SELECT        TOP2 last_Name, salary

FROM            employees

WHERE        department_ID = 'IT'

ORDER BY salary DESC

Asim Ali
by Asim Ali , SAP HCM Support Officer , Al Tilal Steel Company Limited

Here's what nearest to your query ...

 

;WITH T AS

(

SELECT *,

DENSE_RANK() OVER (ORDERBY Salary Desc) AS Rnk

FROM

Employees

)

SELECT Name

FROM T

WHERE Rnk=2;

 

also you can try this one ...

 

SELECT Name

FROM Employees

WHERE Salary = (

SELECT MIN(Salary)

FROM (SELECT DISTINCT TOP (2) Salary

FROM Employees

ORDER BY Salary DESC) T

);

 

Hopefully it helps.

More Questions Like This