ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل مستخدم محذوف‎
تاريخ النشر: 2014/10/03
Prospero Sevilla
من قبل 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
من قبل 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
من قبل Muhammad Waqas , C#/.Net Developer , Signup solution

SELECT        TOP2 last_Name, salary

FROM            employees

WHERE        department_ID = 'IT'

ORDER BY salary DESC

Asim Ali
من قبل 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.

المزيد من الأسئلة المماثلة