MySQL LeetCode 刷题3

目录

1、184部门工资最高的员工

2、185部门工资前三高的全部成员

3、196删除重复的电子邮箱

4、197上升的温度


1、184部门工资最高的员工

编写SQL查询以查找每个部门中薪资最高的员工。按任意顺序返回结果表。

使用join和in语句:

因为 Employee 表包含 Salary 和 DepartmentId 字段,我们可以以此在部门内查询最高工资。因为有可能有多个员工同时拥有最高工资,所以最好在这个查询中不包含雇员名字的信息。然后,我们可以把表 Employee 和 Department 连接,再在这张临时表里用 IN 语句查询部门名字和工资的关系。

select Department.name as 'Department'
,Employee.name as 'Employee'
,salary as 'Salary' from Employee 
inner join Department on departmentId=Department.id 
where (departmentId,salary) 
in (select departmentId,max(salary) 
    from Employee 
    group by departmentId) 

 2、185部门工资前三高的全部成员

公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。

编写一个SQL查询,找出每个部门中 收入高的员工 。以 任意顺序 返回结果表。

Create table If Not Exists Department (id int, name varchar(255));
Truncate table Employee2;
insert into Employee2 (id, name, salary, departmentId) values ('1', 'Joe', '85000', '1');
insert into Employee2 (id, name, salary, departmentId) values ('2', 'Henry', '80000', '2');
insert into Employee2 (id, name, salary, departmentId) values ('3', 'Sam', '60000', '2');
insert into Employee2 (id, name, salary, departmentId) values ('4', 'Max', '90000', '1');
insert into Employee2 (id, name, salary, departmentId) values ('5', 'Janet', '69000', '1');
insert into Employee2 (id, name, salary, departmentId) values ('6', 'Randy', '85000', '1');
insert into Employee2 (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');
SELECT * FROM employee2;
SELECT * FROM Department;

方法:使用join和子查询

first,查询各部门出前三高的工资salary

SELECT e1.name AS employee,e1.salary 
FROM employee e1 
WHERE 3>(
	SELECT COUNT(DISTINCT e2.salary) 
	FROM employee e2 
	where e2.salary>e1.salary
	and e1.departmentId=e2.departmentId)

second,根据各部门前三高的工资筛选行

third,筛选列

SELECT de.name AS Department,e1.name AS employee,e1.salary
FROM employee e1 join Department de ON e1.departmentId=de.id
WHERE 3>(
	SELECT COUNT(DISTINCT e2.salary) 
	FROM employee e2 
	where e2.salary>e1.salary
	and e1.departmentId=e2.departmentId)

 

 3、196删除重复的电子邮箱

编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。

以 任意顺序 返回结果表。 (注意: 仅需要写删除语句,将自动对剩余结果进行查询)

Create table If Not Exists Person2 (Id int, Email varchar(255));
Truncate table Person2;
insert into Person2 (id, email) values ('1', '[email protected]');
insert into Person2 (id, email) values ('2', '[email protected]');
insert into Person2 (id, email) values ('3', '[email protected]');
SELECT * FROM person2;

 

方法:使用delete和where语句,用where语句控制删除其他记录中具有相同电子邮件地址的更大 ID 

DELETE p1 FROM person2 p1,person2 p2 WHERE p1.email=p2.email AND p1.id>p2.id

4、197上升的温度

 编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。

返回结果 不要求顺序 。

Create table If Not Exists Weather (id int, recordDate date, temperature int);
Truncate table Weather;
insert into Weather (id, recordDate, temperature) values ('1', '2015-01-01', '10');
insert into Weather (id, recordDate, temperature) values ('2', '2015-01-02', '25');
insert into Weather (id, recordDate, temperature) values ('3', '2015-01-03', '20');
insert into Weather (id, recordDate, temperature) values ('4', '2015-01-04', '30');
SELECT * FROM Weather;

方法:使用join和datediff()语句,用DateDiff()语句控制天数,DATEDIFF(date1,date2),函数返回date1-date2的结果。

SELECT e1.id FROM Weather e1 
JOIN Weather e2 
ON DATEDIFF(e1.recordDate,e2.recordDate)=1 
AND e1.Temperature>e2.Temperature

 

题目来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/department-top-three-salaries
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/m0_72084056/article/details/126134121