Register now or log in to join your professional community.
Using SQL query, you can try the very simple solution below:
SELECT TOP2 EmployeeName, Salary
FROM EmployeeFile
WHERE Department='IT'
ORDER BY Salary DESC
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"
SELECT TOP2 last_Name, salary
FROM employees
WHERE department_ID = 'IT'
ORDER BY salary DESC
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.