力扣每日刷题_随机个数

目录

力扣_182题

说明

分析

实现

实现方法

小结

力扣_183题

说明

分析

实现

实现方法

小结

力扣_184题

说明

分析

实现

实现方法

小结


  1. 力扣_182题

  1. 说明

https://leetcode.cn/problems/duplicate-emails/submissions/548597503/

  1. 分析

编写解决方案来报告所有重复的电子邮件。 请注意,可以保证电子邮件字段不为 NULL。
以 任意顺序 返回结果表。
输入输出
idemailEmail
1a@b.coma@b.com
2c@d.com
3a@b.com
分析
idemail利用聚合函数,对email分组并根据id计数emailcount(id)找出count((id)>1的email,即为重复的邮件.Email
1a@b.coma@b.com2a@b.com
2c@d.comc@d.com1
3a@b.com
  1. 实现

准备工作

Create table If Not Exists Person (id int, email varchar(255)); 
Truncate table Person; 
insert into Person (id, email) values ('1', 'a@b.com'); 
insert into Person (id, email) values ('2', 'c@d.com'); 
insert into Person (id, email) values ('3', 'a@b.com');
  1. 实现方法

elect email as Email from person group by email having count(id) > 1;
  1. 小结

聚合函数

  1. 力扣_183题

  1. 说明

https://leetcode.cn/problems/customers-who-never-order/submissions/548605636/

  1. 分析

找出所有从不点任何东西的顾客。以 任意顺序 返回结果表。
输入输出
订单表顾客表Customers
idcustomerIdidnameHenry
131JoeMax
212Henry
3Sam
4Max
分析:方法一:子查询
订单表顾客表使用子查询,查出订单表里买过东西的顾客id,然后作为条件在顾客表查询id不等于customeidCustomers
idcustomerIdidnameHenry
131JoeMax
212Henry
3Sam
4Max
分析:方法二:左连接
订单表顾客表使用顾客表左连接订单表,条件是顾客表id=订单表customeid
idcustomerIdidname
131Joe
212Henry
3Sam
4Max
customer.idnameorders.idcustomerIdorders.id为空行对应的custome.id即为从不点任何东西的顾客customer.idnameorders.idcustomerId输出nameCustomers
1Joe212HenrynullnullHenry
2Henrynullnull4MaxnullnullMax
3Sam13
4Maxnullnull
  1. 实现

准备工作 

Create table If Not Exists Customers (id int, name varchar(255));

Create table If Not Exists Orders
(
    id         int,
    customerId int
);
Truncate table Customers;
insert into Customers (id, name)
values ('1', 'Joe');
insert into Customers (id, name)
values ('2', 'Henry');
insert into Customers (id, name)
values ('3', 'Sam');
insert into Customers (id, name)
values ('4', 'Max');
Truncate table Orders;
insert into Orders (id, customerId)
values ('1', '3');
insert into Orders (id, customerId)
values ('2', '1');
  1. 实现方法

  • 方法1:子查询

elect name as Customers from customers where id not in (select customerId from orders);
  • 方法2:左连接

elect name as Customers from customers left join orders on Orders.customerId = Customers.id where Orders.id is null;
  1. 小结

子查询

左连接

  1. 力扣_184题

  1. 说明

https://leetcode.cn/problems/department-highest-salary/submissions/548652398/

  1. 分析

查找出每个部门中薪资最高的员工,按 任意顺序 返回结果表。
输入:employee表输入:department表
idnamesalarydepartmentIdidname
1Joe7000011IT
2Jim9000012Sales
3Henry800002
4Sam600002
5Max900001
分析
idnamesalarydepartmentIdidname
1Joe7000011IT
2Jim9000012Sales
3Henry800002
4Sam600002
5Max900001
首先对两个表进行连接查询,连接条件是员工表的departmentid等于部门表的id,然后利用窗口函数dense_rank(),按照部门名称分组,员工薪资降序排列,查询连接表中的Department.name,Employee.name,salary,ranking得到如下表:
Department.nameEmployee.namesalaryranking
ITJim900001
ITMax900001
ITJoe700002
SalaryHenry800001
SalarySam600002
把上表作为一个临时表,使用子查询,从表中查找ranking=1的Employee.name,结果如下表:
DepartmentEmployeeSalary
ITJim90000
ITMax90000
SalaryHenry80000
  1. 实现

准备工作

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. 实现方法

ith t1 as( select Department.name as Department, Employee.name as Employee, Salary, dense_rank() over (partition by Department.name order by salary desc) as ranking from employee, department where Employee.departmentId = Department.id ) select Department, Employee, Salary from t1 where ranking = 1;
  1. 小结

窗口函数

子查询

多表连接查询

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值