输入: Person表: +----------+----------+-----------+ | personId | lastName | firstName | +----------+----------+-----------+ | 1 | Wang | Allen | | 2 | Alice | Bob | +----------+----------+-----------+ Address表: +-----------+----------+---------------+------------+ | addressId | personId | city | state | +-----------+----------+---------------+------------+ | 1 | 2 | New York City | New York | | 2 | 3 | Leetcode | California | +-----------+----------+---------------+------------+ 输出: +-----------+----------+---------------+----------+ | firstName | lastName | city | state | +-----------+----------+---------------+----------+ | Allen | Wang | Null | Null | | Bob | Alice | New York City | New York | +-----------+----------+---------------+----------+ 解释: 地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。 addressId = 1 包含了 personId = 2 的地址信息。
代码:
1 2 3 4 5
# Write your MySQL query statement below select firstName, lastName, city, state from Person left join Address on Person.personId = Address.personId;
176. 第二高的薪水
题——中等
Employee 表:
1 2 3 4 5 6 7 8
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ 在 SQL 中,id 是这个表的主键。 表的每一行包含员工的工资信息。
# 方法1:使用 排序+limit select (select salary from Employee order by salary desc limit 1,1) as SecondHighestSalary;
# 方法2:去掉最高再求最高 select max(salary) as SecondHighestSalary from Employee where salary < (select max(salary) from Employee);
181. 超过经理收入的员工
题——简单
表:Employee
1 2 3 4 5 6 7 8 9 10
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | salary | int | | managerId | int | +-------------+---------+ id 是该表的主键(具有唯一值的列)。 该表的每一行都表示雇员的ID、姓名、工资和经理的ID。
编写解决方案,找出收入比经理高的员工。
以 任意顺序 返回结果表。
结果格式如下所示。
示例 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
输入: Employee 表: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | Null | | 4 | Max | 90000 | Null | +----+-------+--------+-----------+ 输出: +----------+ | Employee | +----------+ | Joe | +----------+ 解释: Joe 是唯一挣得比经理多的雇员。
代码:
1 2 3 4 5
# Write your MySQL query statement below select a.name Employee from Employee a join Employee b on a.salary > b.salary and a.managerId = b.id;
考虑到SQL语句执行顺序,进行筛选优化
1 2 3 4 5 6
# Write your MySQL query statement below select a.name Employee from Employee a join Employee b on a.salary > b.salary where a.managerId = b.id;
问题
为啥使用子查询后效率差的一批
1 2 3 4 5 6 7 8
# Write your MySQL query statement below select name Employee from Employee e1 where e1.salary >( select salary from Employee e2 where e2.id = e1.managerID )
182. 查找重复的电子邮箱
题目——简单
表: Person
1 2 3 4 5 6 7 8
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id 是该表的主键(具有唯一值的列)。 此表的每一行都包含一封电子邮件。电子邮件不包含大写字母。
# Write your MySQL query statement below select distinct p1.email from person p1 left join person p2 on p1.id <> p2.id where p1.email = p2.email;
方法二:分组(后来突然自己想到的一种方法)
虽然效率低,单字节想到的哦
1 2 3 4 5
# Write your MySQL query statement below select email Email from Person group by email having count(*) > 1;
183. 从不订购的客户
题目——简单
Customers 表:
1 2 3 4 5 6 7 8
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+ 在 SQL 中,id 是该表的主键。 该表的每一行都表示客户的 ID 和名称。
Orders 表:
1 2 3 4 5 6 7 8 9
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | customerId | int | +-------------+------+ 在 SQL 中,id 是该表的主键。 customerId 是 Customers 表中 ID 的外键( Pandas 中的连接键)。 该表的每一行都表示订单的 ID 和订购该订单的客户的 ID。
dense_rank() over 后面跟排序的依据的列,下面是用了一个排序好的列(order by score desc)。 注意:如果select中有一列是用rank()这类函数,其他的列都会按着他这列规定好的顺序排。
代码
1 2 3
# Write your MySQL query statement below select score, dense_rank() over (order by score desc) 'rank' from Scores;
180. 连续出现的数字
题目——中等
表:Logs
1 2 3 4 5 6 7 8
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ 在 SQL 中,id 是该表的主键。 id 是一个自增列。
输入: Employee 表: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Jim | 90000 | 1 | | 3 | Henry | 80000 | 2 | | 4 | Sam | 60000 | 2 | | 5 | Max | 90000 | 1 | +----+-------+--------+--------------+ Department 表: +----+-------+ | id | name | +----+-------+ | 1 | IT | | 2 | Sales | +----+-------+ 输出: +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Jim | 90000 | | Sales | Henry | 80000 | | IT | Max | 90000 | +------------+----------+--------+ 解释:Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。
思路
按部门Id分组并选出部门最大薪资的部门Id和salary
用连接表判断部门Id和salary是否在1表中
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Write your MySQL query statement below select d.name Department, e.name Employee, Salary from Employee e join Department d on departmentId=d.id where (departmentId, Salary) in ( select departmentId, Max(Salary) from Employee group by departmentId );