CREATE DATABASE test;
use test;
CREATE TABLE Employee (
id int,
name varchar(255),
salary int,
departmentId int
);
CREATE TABLE Department (
id int,
name varchar(255)
);
INSERT INTO Employee VALUES (1, 'joe', 85000, 1);
INSERT INTO Employee VALUES (2, 'Henry', 80000, 2);
INSERT INTO Employee VALUES (3, 'Sam ', 60000, 2);
INSERT INTO Employee VALUES (4, 'Max', 90000, 1);
INSERT INTO Employee VALUES (5, 'jet', 69000, 1);
INSERT INTO Employee VALUES (6, 'Randy', 90000, 1);
INSERT INTO Employee VALUES (7, 'Will', 70000, 1);
INSERT INTO Department VALUES (1, 'IT');
INSERT INTO Department VALUES (2, 'sales');
select
t2.name as Department,
t1.name as Employee,
t1.salary as Salary
from
(
select name,departmentId,salary,
DENSE_RANK() OVER (partition by departmentId ORDER BY salary DESC) as s1
from Employee
)as t1
inner join Department as t2
on t2.id=t1.departmentId
where t1.s1<=3
;