LeetCode-数据库184-185
184题干:
编写SQL查询以查找每个部门中薪资最高的员工。
按 任意顺序 返回结果表。
SQL架构:
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))
Truncate table Employee
insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '70000', '1')
insert into Employee (id, name, salary, departmentId) values ('2', 'Jim', '90000', '1')
insert into Employee (id, name, salary, departmentId) values ('3', 'Henry', '80000', '2')
insert into Employee (id, name, salary, departmentId) values ('4', 'Sam', '60000', '2')
insert into Employee (id, name, salary, departmentId) values ('5', 'Max', '90000', '1')
Truncate table Department
insert into Department (id, name) values ('1', 'IT')
insert into Department (id, name) values ('2', 'Sales')
示例:
我的题解
1 SELECT D.name AS 'Department', 2 E.name AS 'Employee', 3 E.salary AS 'Salary' 4 FROM Employee E LEFT JOIN Department D ON 5 E.departmentId = D.id 6 WHERE (E.departmentId,E.salary) IN 7 (SELECT departmentId, MAX(salary) 8 FROM Employee 9 GROUP BY departmentId)
优秀题解
1 # Write your MySQL query statement below 2 #对于不同的部门,我们要分别找,其中工资最高的员工,然后不要去重 3 #先找到一个部门内工作最高的,它的部门和工资数目 4 # SELECT 5 # DepartmentId, MAX(Salary) 6 # FROM 7 # Employee 8 # GROUP BY DepartmentId; 9 10 #然后把表 Employee 和 Department 连接,再在这张临时表里用 IN 语句查询部门名字和工资的关系。 11 12 select 13 Department.name as 'Department', 14 Employee.name as 'Employee', 15 salary as Salary 16 FROM 17 Employee JOIN Department 18 ON Employee.departmentid = Department.id #拼接条件 19 WHERE #从这里开始设置查询条件 20 (Employee.departmentid,salary) IN 21 ( 22 select Employee.departmentid, max(salary) 23 FROM 24 Employee 25 GROUP BY DepartmentId 26 );
185题干:
公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。
编写一个SQL查询,找出每个部门中 收入高的员工 。
以 任意顺序 返回结果表。
SQL架构
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)) Truncate table Employee insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '85000', '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 Employee (id, name, salary, departmentId) values ('5', 'Janet', '69000', '1') insert into Employee (id, name, salary, departmentId) values ('6', 'Randy', '85000', '1') insert into Employee (id, name, salary, departmentId) values ('7', 'Will', '70000', '1') Truncate table Department insert into Department (id, name) values ('1', 'IT') insert into Department (id, name) values ('2', 'Sales')
示例
我的题解
1 # Write your MySQL query statement below 2 SELECT D.name AS 'Department', 3 E.name AS 'Employee', 4 E.salary AS 'Salary' 5 FROM Employee E LEFT JOIN Department D ON 6 E.departmentId = D.id 7 WHERE (E.departmentId,E.salary) IN 8 (SELECT P.departmentId,P.salary 9 FROM(SELECT departmentId, name,salary, 10 DENSE_RANK() OVER (PARTITION BY departmentId ORDER BY salary DESC) AS 'RANK' 11 FROM Employee) P 12 WHERE P.RANK <= 3)
优秀题解
不使用窗口函数
1 SELECT 2 d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary 3 FROM 4 Employee e1 5 JOIN 6 Department d ON e1.DepartmentId = d.Id 7 WHERE 8 3 > (SELECT 9 COUNT(DISTINCT e2.Salary) 10 FROM 11 Employee e2 12 WHERE 13 e2.Salary > e1.Salary 14 AND e1.DepartmentId = e2.DepartmentId 15 ) 16 ; 17 18 作者:LeetCode 19 链接:https://leetcode-cn.com/problems/department-top-three-salaries/solution/bu-men-gong-zi-qian-san-gao-de-yuan-gong-by-leetco/ 20 来源:力扣(LeetCode) 21 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
使用窗口函数
1 # Write your MySQL query statement below 2 select d.name Department ,t.name Employee ,t.salary Salary 3 from 4 ( 5 select name, 6 salary, 7 dense_rank() over(partition by departmentId order by Salary desc) rk, 8 departmentId 9 from Employee 10 )t 11 join Department d on t.departmentId =d.id 12 where t.rk<=3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/department-top-three-salaries
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。