clear markdown help donate comments/suggestions/bugs a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith?
By using dbfiddle, you agree to license everything you submit by Creative Commons CC0

Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);

Create table If Not Exists Department (Id int, Name varchar(255));

insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1');

insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2');

insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2');

insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1');

insert into Department (Id, Name) values ('1', 'IT');

insert into Department (Id, Name) values ('2', 'Sales');

SELECT * FROM ( SELECT d.Name as Department, e.Name as Employee, Salary, DENSE_RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) rank FROM Employee e JOIN Department d ON e.DepartmentId = d.id ) t WHERE rank <= 3 ORDER BY Department, rank;
Department Employee Salary rank
IT Max 90000 1
IT Joe 70000 2
Sales Henry 80000 1
Sales Sam 60000 2