clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1130817 fiddles created (16564 in the last week).

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


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


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


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


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


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


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


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


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
 hidden batch(es)