Download sample data with Schema |
I have three tables mentioned below:
- Employee – Master table
- SalaryDetails – Reference table with salary information
- Department – Master table
The sample data into these tables is show below:
Employee |
Department
|
Salary Details
|
Resultant Data
|
Schema with sample data is provided on download link, which will save your time for preparing sample tables and data. Click here to download.
Following are the solutions provided by:
Me (Chirag Vidani)
select data.DepartmentName 'Department Name', data.EmployeeName 'Employee Name', data.Salary 'Salary/Day' from (select d.Name 'DepartmentName',e.Name 'EmployeeName',sd.Salary 'Salary',dense_rank() over(partition by sd.DepartmentId order by sd.Salary desc)'SalaryRank' from Salarydetails sd join Department d on sd.DepartmentId = d.Id join Employee e on sd.EmployeeID =e.Id) as data where data.SalaryRank <= 2 order by data.DepartmentName,data.Salary desc
Mr. Kaushik Patel
SELECT d.* FROM (select S.*,D.NAME DepartmentName,E.NAME EmployeeName ,RANK() OVER (PARTITION BY DeptID ORDER BY SALARY DESC) AS Raw_Rank from Salary s inner join Employee e on s.EmployeeId = e.id inner join Department d on s.DepartmentId = d.id ) d WHERE Raw_Rank <=2
|
|
TRY BELOW :::
ReplyDeleteSELECT d.NAME DepartmentName,e.NAME EmployeeName,F.Salary
FROM (
SELECT *, row_number() OVER (PARTITION BY DepartmentId ORDER BY Salary desc) salaryOrder
FROM Salary
) F
INNER JOIN Department d ON d.id = F.DepartmentId
INNER JOIN Employee e ON e.id = F.EmployeeId
WHERE F.salaryOrder<3
ORDER BY d.name
NARENDRA ZALA
Hello All,
ReplyDeleteMy solution is as follows:
select
data.DepartmentName 'Department Name', data.EmployeeName 'Employee Name', data.Salary 'Salary/Day'
from
(select d.Name 'DepartmentName',e.Name 'EmployeeName',sd.Salary 'Salary',dense_rank() over(partition by sd.DepartmentId order by sd.Salary desc)'SalaryRank'from Salarydetails sd
join
Department d on sd.DepartmentId = d.Id
join
Employee e on sd.EmployeeID =e.Id) as data
where
data.SalaryRank <= 2
order by
data.DepartmentName,data.Salary desc
This comment has been removed by a blog administrator.
ReplyDeleteAnother solution by Mr. Kaushik Patel is as given below:
ReplyDeleteSELECT d.* FROM( select S.*,D.NAME DepartmentName,E.NAME EmployeeName ,RANK() OVER (PARTITION BY DeptID ORDER BY SALARY DESC) AS Raw_Rank from Salary s inner join Employee e on s.EmployeeId = e.id inner join Department d on s.DepartmentId = d.id ) d WHERE Raw_Rank <=2
Hello Narendra,
ReplyDeleteYour query will not give result as expected.
In the case where 1 or more persons have same salary in same department.