Let's say I have this table called Employees
:
Id | Name | Salary | DepartmentId |
---|---|---|---|
1 | Joe | 85000 | 1 |
2 | Henry | 80000 | 2 |
3 | Sam | 60000 | 2 |
4 | Max | 90000 | 1 |
5 | Janet | 69000 | 1 |
6 | Randy | 85000 | 1 |
7 | Will | 70000 | 1 |
And for each department id, I want the top three salaries. So the result should be:
DepartmentId | Name | Salary |
---|---|---|
1 | Max | 90000 |
1 | Joe | 85000 |
1 | Randy | 85000 |
1 | Will | 70000 |
2 | Henry | 80000 |
2 | Sam | 60000 |
So for each department id, the top three salaries are returned, and if there are duplicate salaries in the top three, the duplicates are returned too and the limiting factor is top three unique salaries. How to implement this?