目录
一、182. 查找重复的电子邮箱 - 力扣(LeetCode)
三、184. 部门工资最高的员工 - 力扣(LeetCode)
一、182. 查找重复的电子邮箱 - 力扣(LeetCode)
采用分组,把邮件名称相同的放到一组 having统计这组的个数,个数大于1则证明这个电子邮件是重复的
select email as 'Email'
from person
group by email
having count(email) > 1
二、183. 从不订购的客户 - 力扣(LeetCode)
一个顾客表
一个购买表
顾客表左连接购买表 保留左边的表
右边的列数据为空就可以表明没有购买过
判断为null 可以使用ifnull 或者is null
select c.name as 'Customers'
from Customers c
left join orders o on c.id = o.customerId
where ifnull(o.id,0) = 0 and ifnull(o.customerId,0) = 0;
三、184. 部门工资最高的员工 - 力扣(LeetCode)
先用分组和max函数查出每个部门薪水的最大值是多少 因为知道了最大值以后就能把重复的数据保留下来,最大薪水的可能不仅仅只有一个人,而是多个人,但是知道了最大的薪水值就好办了
(select departmentId,max(salary)
as 'salary' from Employee group by departmentId)
这个中间结果和其他两个表在做连接,这里使用inner join 我试了普通的笛卡尔积是超时的
select d1.name as 'Department',e1.name as 'Employee',e1.salary as 'salary'
from (select departmentId,max(salary) as 'salary' from Employee group by departmentId) ee join Employee e1 on ee.departmentId = e1.departmentId and ee.salary = e1.salary
join Department d1 on e1.departmentId = d1.id