基础及前5题的综合练习见:组队学习sql之基础+综合练习1
6~8题见:组队学习sql之综合练习2
MySQL Leetcode练习
Task06综合练习一共11题,本篇实现9~11。
将项目7中的employee表清空,重新插入以下数据(其实是多插入5,6两行):
185. 部门工资前三高的所有员工
题目
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+
编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
此外,请考虑实现各部门前N高工资的员工功能。
解答
- 思路1
SELECT t.Name Department,t11.Name Employee,t11.Salary
FROM Employee t11 JOIN Department t ON t11.DepartmentId = t.Id
WHERE 3>(SELECT COUNT(DISTINCT t1.Salary) FROM Employee t1 WHERE t1.Salary>t11.Salary AND t1.DepartmentId =t11.DepartmentId )
ORDER BY t.Name ASC,t11.Salary DESC
- 思路2 利用排序窗口函数
SELECT t2.Name AS Department, t1.Name AS 'employee',Salary
FROM (
SELECT *, DENSE_RANK() OVER (PARTition by DepartmentId ORDER BY Salary DESC) AS "rank"
FROM Employee ) t1 INNER JOIN Department t2
ON t1.DepartmentId = t2.Id
WHERE t1.rank < 4;
612 平面上的最近距离
题目
point_2d表包含一个平面内一些点(超过两个)的坐标值(x,y)。
写一条查询语句求出这些点中的最短距离并保留2位小数。
|x | y |
|----|----|
| -1 | -1 |
| 0 | 0 |
| -1 | -2 |
最短距离是1,从点(-1,-1)到点(-1,2)。所以输出结果为:
| shortest |
1.00
+--------+
|shortest|
+--------+
|1.00 |
+--------+
**注意:**所有点的最大距离小于10000。
解答
思路:两个表不等值联结后计算点间距离,然后求极值
select round(sqrt(min(power((p1.x-p2.x),2)+power((p1.y-p2.y),2))),2) as shortest
from point_2d p1,point_2d p2
where p1.x<>p2.x or p1.y<>p2.y;
262. 行程和用户
题目
Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。
Id | Client_Id | Driver_Id | City_Id | Status | Request_at |
---|---|---|---|---|---|
1 | 1 | 10 | 1 | completed | 2013-10-1 |
2 | 2 | 11 | 1 | cancelled_by_driver | 2013-10-1 |
3 | 3 | 12 | 6 | completed | 2013-10-1 |
4 | 4 | 13 | 6 | cancelled_by_client | 2013-10-1 |
5 | 1 | 10 | 1 | completed | 2013-10-2 |
6 | 2 | 11 | 6 | completed | 2013-10-2 |
7 | 3 | 12 | 6 | completed | 2013-10-2 |
8 | 2 | 12 | 12 | completed | 2013-10-3 |
9 | 3 | 10 | 12 | completed | 2013-10-3 |
10 | 4 | 13 | 12 | cancelled_by_driver | 2013-10-3 |
Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。
+----------+--------+--------+
| 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)保留两位小数。
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
解答
- 思路1 不知道为什么就对了,碰巧
SELECT t.Request_at AS 'Day',ROUND(SUM(CASE WHEN t.Status IN ('cancelled_by_driver','cancelled_by_client') THEN 1 ELSE 0 END) / COUNT(1),2) AS 'Cancellation Rate'
FROM (
SELECT *
FROM Trips P LEFT JOIN Users Q
ON P.Client_Id = Q.Users_Id
WHERE Q.Banned != 'Yes' AND P.Request_at BETWEEN '2013-10-01' AND '2013-10-03') t
GROUP BY Request_at
- 思路2 首先排除所有由禁止用户的driver,client组合,然后计算取消率
SELECT t.Request_at AS 'Day',ROUND(SUM(CASE WHEN t.Status IN ('cancelled_by_driver','cancelled_by_client') THEN 1 ELSE 0 END) / COUNT(1),2) AS 'Cancellation Rate'
FROM (SELECT * FROM Trips WHERE (Client_Id,Driver_Id) NOT IN
(SELECT p1.Users_Id,p2.Users_Id FROM Users p1,Users p2 WHERE p1.Banned='Yes' OR p2.Banned ='Yes') AND Request_at BETWEEN '2013-10-01' AND '2013-10-03') t
GROUP BY Request_at
THE END.
team-learning-sql website:
https://github.com/datawhalechina/team-learning-sql