MySQL笔记

作为学习MySQL的笔记

语法

Leetcode题目

175. 组合两个表

题目

表1: Person

1
2
3
4
5
6
7
8
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId 是上表主键

表2: Address

1
2
3
4
5
6
7
8
9
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

FirstName, LastName, City, State

思路

使用LEFT JOIN 按照 PersonId 合并即可

代码

1
2
3
4
5
6
# Write your MySQL query statement below
SELECT FirstName, LastName, City, State
FROM Person
LEFT JOIN address
ON address.PersonId = Person.PersonId
;

176. 第二高的薪水

题目

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

1
2
3
4
5
6
7
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

1
2
3
4
5
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

思路

我们先找最大的,然后在比最大小的里面比找最大的

代码

1
2
3
4
5
# Write your MySQL query statement below
SELECT max(Salary)
from Employee
where Salary < (SELECT max(Salary) from Employee)
;

177. 第N高的薪水

题目

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

1
2
3
4
5
6
7
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

1
2
3
4
5
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+

思路

先排序,取前N个,然后取最小,最后再判断是否存在即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
SELECT IF(ct<N,NULL,minv) FROM
(
SELECT COUNT(1) as ct,MIN(Salary) as minv FROM
(
SELECT DISTINCT Salary FROM
Employee ORDER BY Salary DESC LIMIT N
) as b

) as a
);
END

178. 分数排名

题目

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

1
2
3
4
5
6
7
8
9
10
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

1
2
3
4
5
6
7
8
9
10
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+

思路

我们首先需要去重,然后对于某个分值,计算比小于等于它的有多少个。

我们再把两个表联合起来,再排序即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Write your MySQL query statement below
SELECT s.Score,t.Rank
FROM Scores s
LEFT JOIN
(SELECT a.v, COUNT(b.v) as Rank
FROM
(SELECT DISTINCT(Score) as v
FROM Scores ) a,(SELECT DISTINCT(Score) as v
FROM Scores ) b
WHERE a.v<=b.v
GROUP BY a.v
) as t
ON t.v=s.Score
ORDER BY s.Score DESC
;

180. 连续出现的数字

题目

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

1
2
3
4
5
6
7
8
9
10
11
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

1
2
3
4
5
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+

思路

我们直接考虑连续3个即可

代码

1
2
3
4
5
6
7
8
9
10
# Write your MySQL query statement below

SELECT DISTINCT(l1.Num) AS ConsecutiveNums

FROM Logs l1,Logs l2,logs l3
WHERE l1.Id=l2.Id-1
AND l2.Id=l3.Id-1
AND l1.Num=l2.Num
AND l2.Num=l3.Num
;

181. 超过经理收入的员工

题目

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

1
2
3
4
5
6
7
8
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

1
2
3
4
5
+----------+
| Employee |
+----------+
| Joe |
+----------+

思路

我们使用where过滤即可

代码

1
2
3
4
5
6
# Write your MySQL query statement below
SELECT a.Name AS Employee
FROM Employee a,Employee b
WHERE a.ManagerId = b.ID
AND a.Salary > b.Salary
;

182. 查找重复的电子邮箱

题目

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

1
2
3
4
5
6
7
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

1
2
3
4
5
+---------+
| Email |
+---------+
| a@b.com |
+---------+

说明:所有电子邮箱都是小写字母。

思路

统计一下每个的数目,然后找出现次数大于1的即可

代码

1
2
3
4
5
6
7
# Write your MySQL query statement below
SELECT Email FROM
(SELECT Email, COUNT(Email) as num
from Person
Group by Email) as temp_table
WHERE num > 1
;

183. 从不订购的客户

题目

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

1
2
3
4
5
6
7
8
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+

Orders 表:

1
2
3
4
5
6
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+

例如给定上述表格,你的查询应返回:

1
2
3
4
5
6
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+

思路

我们把两个表和再一起然后判断一下那些的CustomerId是NULL即可

代码

1
2
3
4
5
6
7
# Write your MySQL query statement below
SELECT Name AS Customers
FROM Customers c
LEFT JOIN Orders o
ON c.Id = o.CustomerId
WHERE CustomerId IS NULL
;

184. 部门工资最高的员工

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

1
2
3
4
5
6
7
8
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

1
2
3
4
5
6
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

1
2
3
4
5
6
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+

思路

我们先找每个部门最大的,然后再联合到一起找是最大的即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT t2.dname AS Department, t2.name as Employee,t2.Salary
FROM
(
SELECT e2.name,e2.Salary,t.maxs,e2.DepartmentId,dname
FROM Employee e2
LEFT JOIN
(SELECT e.DepartmentId as did,MAX(Salary) as maxs,d.name AS dname
FROM Employee e
LEFT JOIN Department d
ON e.DepartmentId=d.Id
GROUP BY e.DepartmentId
) as t
ON e2.DepartmentId=t.did
) as t2
WHERE Salary=maxs
AND t2.dname IS NOT NULL
;

185. 部门工资前三高的学生

题目

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

1
2
3
4
5
6
7
8
9
10
11
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

1
2
3
4
5
6
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

1
2
3
4
5
6
7
8
9
10
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

思路

我们首先求比某个元素大的出现的次数(在同一个部门),然后选小于3的,最后联合在一起即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Write your MySQL query statement below

SELECT Department.Name as Department,Employee,Salary
FROM
(
SELECT a.DepartmentId AS Id,a.Name AS Employee,a.Salary AS Salary
FROM
Employee a
WHERE 3 > (
SELECT COUNT(DISTINCT b.Salary)
FROM
Employee b
WHERE a.DepartmentId=b.DepartmentId
AND a.Salary < b.Salary
)
ORDER BY DepartmentId
)as t
JOIN Department
ON Department.Id=t.Id
;

196. 删除重复的电子邮箱

题目

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

1
2
3
4
5
6
7
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+

Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

1
2
3
4
5
6
7
8


+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+

提示:

执行 SQL 之后,输出是整个 Person 表。
使用 delete 语句。

思路

按题意即可

代码

1
2
3
4
5
6
7
# Write your MySQL query statement below
DELETE p1.*
FROM Person p1,
Person p2
WHERE
p1.Email = p2.Email AND p1.ID > p2.ID
;

197. 上升的温度

题目

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

1
2
3
4
5
6
7
8
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+

例如,根据上述给定的 Weather 表格,返回如下 Id:

1
2
3
4
5
6
+----+
| Id |
+----+
| 2 |
| 4 |
+----+

思路

直接按题意即可

代码

1
2
3
4
5
# Write your MySQL query statement below
SELECT DISTINCT(a.Id)
FROM Weather a,Weather b
WHERE dateDiff(a.RecordDate,b.RecordDate)=1
AND a.Temperature >b.Temperature

262. 行程和用户

题目

Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|
| 3 | 3 | 12 | 6 | completed |2013-10-01|
| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|
| 5 | 1 | 10 | 1 | completed |2013-10-02|
| 6 | 2 | 11 | 6 | completed |2013-10-02|
| 7 | 3 | 12 | 6 | completed |2013-10-02|
| 8 | 2 | 12 | 12 | completed |2013-10-03|
| 9 | 3 | 10 | 12 | completed |2013-10-03|
| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+

Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

1
2
3
4
5
6
7
8
9
10
11
12
+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)

1
2
3
4
5
6
7
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+

思路

我们首先把被ban的去掉,然后直接求即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Write your MySQL query statement below
SELECT Date AS Day, round(SUM(IF(Status='completed',0,1))/COUNT(Status) ,2) as `Cancellation Rate`
FROM
(
SELECT Status,date FROM
(
SELECT Status,date,band1,Users.Banned AS band2
FROM
(
SELECT Status,Request_at AS date, Driver_Id, Users.Banned AS band1
FROM
Trips

JOIN Users
On Users_Id=Client_Id
) as tmp
JOIN Users
On Driver_Id=Users_Id
) as tmp1
WHERE band1!='Yes'
AND band2!='Yes'
) AS t
WHERE
date BETWEEN '2013-10-01' AND '2013-10-03'
GROUP By date
;