# Write your MySQL query statement below
select b.name as Department, a.name as Employee, a.salary as Salary
from Employee as a, Department as b, (
select max(salary) as max, departmentId
from Employee
group by departmentId) as f
where a.departmentId = b.id and f.max = a.salary and f.departmentId = b.id;
根据departmentId进行分组,找到每一个分组的最大薪资,设为临时表f
使用group by语句,max的子查询建表
# Please write a DELETE statement and DO NOT write a SELECT statement.
# Write your MySQL query statement below
delete a
from Person as a, Person as b
where a.email = b.email and a.id > b.id
#学会使用删除语句
#类似于将select a.*变成delete a
#当我们需要删除表中的重复数据时,可以使用自连接和WHERE子句来实现。自连接是指将一个表与自身连接起来,这样我们就可以在同一张表中进行比较和筛选。
#在这个例子中,我们需要删除Person表中重复的电子邮件,只保留一个id最小的唯一电子邮件。我们可以使用自连接将Person表与自身连接起来,然后使用WHERE子句过滤掉重复的电子邮件,并且保留id最小的电子邮件。具体来说,这个语句的含义是:
#从Person表中选择两个不同的行,用p1和p2表示。在这两个行中,如果它们的email相同且p1的id大于p2的id,则删除p1行。
# Write your MySQL query statement below
select b.id
from Weather as a, Weather as b
where DATEDIFF(b.recordDate, a.recordDate)=1 and a.Temperature<b.Temperature
#比较日期,要用与日期相关的函数,本题应该使用datediff(date1, date2)=1
#DATEDIFF(date1, date2) #date1-date2