Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Utilisateur supprimé
Date de publication: 2014/10/03
Prospero Sevilla
par 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
par 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
par Muhammad Waqas , Dot Net and MVC Developer , Osys Technologies

SELECT        TOP2 last_Name, salary

FROM            employees

WHERE        department_ID = 'IT'

ORDER BY salary DESC

Asim Ali
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?