LeetCode-SQL练习题总结(MySQL实现)

LeetCode上的SQL练习题一共有十九道,题目网址:https://leetcode-cn.com/problemset/database/ 

本文将对其中的部分题做一个总结。

176.查询第二高的薪水:

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

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

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

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

方法一:

select (select distinct Salary from Employee order by Salary desc limit 1,1) as SecondHighestSalary;
# Salary数据有可能有重复的,所以要去重。

方法二:

select ifnull((select distinct Salary from Employee order by Salary desc limit 1,1),null) as SecondHighestSalary;
# 加个ifnull函数后,SQL语句的可能性更强。

177.查询第N高的薪水:

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

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

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

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

代码:

# 创建存储函数:
create function getNthHighestSalary1(N INT) 
    returns int 
    deterministic 
begin 
    declare x int; 
    set x = N-1;  
    return (select distinct Salary as NthHighestSalary from Employee1 order by Salary desc limit x,1);
end;

# 调用存储函数:
select getNthHighestSalary(2)

178.查询分数排名:

关于排名问题,详见:https://blog.csdn.net/qq_41080850/article/details/84858522

180.查询连续出现的数字:

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

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

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

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+

方法一:利用自连接

select distinct l1.Num as ConsecutiveNums from logs l1 inner join logs l2 
on l1.Id = l2.Id-1 inner join logs l3 on l2.Id = l3.Id-1 
where l1.Num = l2.Num and l2.Num = l3.Num;
# 代码中的distinct是为了去除查询结果中的重复值

方法二:利用自关联

select t1.Num as ConsecutiveNums
from logs as t1, logs as t2, logs as t3
where t1.Id = t2.Id-1 and t2.Id = t3.Id-1 and t1.Num = t2.Num and t2.Num = t3.Num
group by t1.Num;
# 代码中的group by是为了去除查询结果中的重复值

方法三:利用case when函数和用户变量

select distinct Num as ConsecutiveNums
from (
  select Num, 
    case 
      when @prev = Num then @count := @count + 1
      when (@prev := Num) is not null then @count := 1
    end as CNT
  from Logs, (select @prev := null,@count := null) as t
) as temp
where temp.CNT >= 3;

总结:方法一和方法二的原理相同,都是基于连续出现的数字,它们的Id值相差为1。方法三是创建一个用户变量用以对数字连续出现的次数计数,再以计数不小于3为进一步筛选出符合要求的数字。相比方法一和方法二,方法三的适用范围更广,而方法一和方法二适用于数据量较小或自连接/自关联次数较少的情况。

196.删除重复的电子邮箱:

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

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
| 3  | [email protected] |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
+----+------------------+

代码:

# 先找到要删除的记录:
select p1.*
from Person as p1,
     Person as p2
where
    p1.Email = p2.Email and p1.Id > p2.Id

# 再将select改为delete:
delete p1
from Person as p1,Person as p2  
where p1.Email = p2.Email and p1.Id > p2.Id

197.上升的温度:

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

+---------+------------------+------------------+
| 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:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+

方法一:

select w1.Id as Id from Weather w1 join Weather w2 
on w2.RecordDate=date_sub(w1.RecordDate,interval 1 day) 
where w1.Temperature>w2.Temperature;

方法二:

select w1.Id as Id from Weather as w1 left join Weather as w2 
on DATEDIFF(w1.RecordDate,w2.RecordDate)=1 
where w1.Temperature>w2.Temperature;

方法三:

select w1.Id as Id from Weather as w1 left join Weather as w2 
on w1.RecordDate-w2.RecordDate=1 
where w1.Temperature>w2.Temperature;

思考与总结:
左连接 on不仅可以以两个要左连接的表中的某两个同类字段的值是否相等为匹配条件,还可以以这两个同类字段经过某种运算后的结果为匹配条件。例如本题中就是以两个表中的日期字段RecordDate的差是否为1作为匹配条件。

626.换座位:

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

示例:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

假如数据输入的是上表,则输出结果如下:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

注意:

如果学生人数是奇数,则不需要改变最后一个同学的座位。

代码:

# 思路:利用Id的奇偶性交换相邻学生的Id,然后再对Id重新排序。

select
case when id%2=0 then id-1
when id<(select max(id) from seat) then id+1
else id
end as id,student from seat order by id;

184.查询部门工资最高的员工:

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

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

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

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

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

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

代码:

select d.Name as Department, e.Name as Employee, e.Salary 
from Department d left join new_employee e on d.Id = e.DepartmentId 
and e.Salary = (select max(Salary) from new_employee where DepartmentId = d.Id)

185.查询部门工资前三高的员工:

详见:https://my.oschina.net/Tsybius2014/blog/649612

此外,还可以参考我的另一篇文章:https://blog.csdn.net/qq_41080850/article/details/89007142

181.查询超过经理收入的员工:

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

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

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

+----------+
| Employee |
+----------+
| Joe      |
+----------+

代码:

select t2.name as Employee from Employee as t1 left join Employee as t2 
on t1.Id = t2.ManagerId where t1.Salary < t2.Salary;

601.查询体育馆的人流量的高峰时段:

详见:https://blog.csdn.net/qq_41080850/article/details/84893530

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-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’)的枚举类型。

+----------+--------+--------+
| 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        |
+------------+-------------------+

方法一:

# 思路:把非禁止用户的信息记录和非禁止用户中交易状态为未完成的信息记录分别放入两张表中,然后在
# 两表左连接的同时计算出各个日期对应的非禁止用户的取消率。

select t1.Request_at as Day,IFNULL(round(t2.num/t1.num,2),0) as 'Cancellation Rate'
from 
(
    select Request_at,count(Request_at) as num
    from Trips
    left join Users on Users.Users_Id = Trips.Client_Id 
    where Users.Banned = 'No'
    and Trips.Request_at between '2013-10-01' and '2013-10-03'
    group by Request_at
) as t1
left join 
(
    select Request_at,count(Request_at) as num
    from Trips
    left join Users on Users.Users_Id = Trips.Client_Id 
    where Users.Banned = 'No'
    and Trips.Request_at between '2013-10-01' and '2013-10-03'
    and Trips.Status != 'completed'
    group by Request_at
) as t2
on t1.Request_at = t2.Request_at;

方法二:

# 基本思路:先筛选出非禁止用户的信息记录,再利用聚合函数sum与count计算各个日期对应的取消率。

select Request_at as Day,round(sum(Status!='completed')/count(Status),2) as 'Cancellation Rate' 
from trips where trips.Client_Id in (select Users_Id from users where banned != 'Yes')
and trips.Driver_Id in (select Users_Id from users where banned != 'Yes') 
and Request_at between '2013-10-01' and '2013-10-03' 
group by Request_at;

PS:本文为博主原创文章,转载请注明出处。

你可能感兴趣的:(MySQL)