LeetCode数据库SQL题目记录(难度:简单)

难度:简单

目录

175. 组合两个表

176. 第二高的薪水

181. 超过经理收入的员工

182. 查找重复的电子邮箱

183. 从不订购的客户

196. 删除重复的电子邮箱

197. 上升的温度

511. 游戏玩法分析 I

512. 游戏玩法分析 II

577. 员工奖金

584. 寻找用户推荐人

586. 订单最多的客户

595. 大的国家

596. 超过5名学生的课

597. 好友申请 I:总体通过率

603. 连续空余座位

607. 销售员

610. 判断三角形

613. 直线上的最近距离

619. 只出现一次的最大数字

620. 有趣的电影

627. 变更性别

1050. 合作过至少三次的演员和导演

1068. 产品销售分析 I

1069. 产品销售分析 II

1075. 项目员工 I

1076. 项目员工II

1082. 销售分析 I

1083. 销售分析 II

1084. 销售分析III

1113. 报告的记录

1141. 查询近30天活跃用户数

1142. 过去30天的用户活动 II

1148. 文章浏览 I

1173. 即时食物配送 I

1179. 重新格式化部门表

1211. 查询结果的质量和占比

1241. 每个帖子的评论数

1251. 平均售价

1280. 学生们参加各科测试的次数

1294. 不同国家的天气类型

1303. 求团队人数

1322. 广告效果

1327. 列出指定时间段内所有的下单产品

1350. 院系无效的学生

1378. 使用唯一标识码替换员工ID

1407. 排名靠前的旅行者

1435. 制作会话柱状图

1485. 按日期分组销售产品

1495. 上月播放的儿童适宜电影

1511. 消费者下单频率

1517. 查找拥有有效邮箱的用户

1527. 患某种疾病的患者

1543. 产品名称格式修复

1565. 按月统计订单数与顾客数

1571. 仓库经理

1581. 进店却未进行过交易的顾客

1587. 银行账户概要 II

1607. 没有卖出的卖家

1623. 三人国家代表队

1633. 各赛事的用户注册率

1661. 每台机器的进程平均运行时间

1667. 修复表中的名字

1677. 发票中的产品金额

1683. 无效的推文

1693. 每天的领导和合伙人

1729. 求关注者的数量

1731. 每位经理的下属员工数量

1741. 查找每个员工花费的总时间

1757. 可回收且低脂的产品

1777. 每家商店的产品价格

1789. 员工的直属部门

1795. 每个产品在不同商店的价格

1809. 没有广告的剧集

1821. 寻找今年具有正收入的客户

1873. 计算特殊奖金

1890. 2020年最后一次登录


175. 组合两个表

LeetCode数据库SQL题目记录(难度:简单)_第1张图片

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

FirstName, LastName, City, State

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combine-two-tables
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select FirstName, LastName, City, State
from Person a left join Address b 
on a.PersonId = b.PersonId

176. 第二高的薪水

LeetCode数据库SQL题目记录(难度:简单)_第2张图片

# Write your MySQL query statement below
select (select distinct Salary from Employee order by Salary desc limit 1,1) SecondHighestSalary 

181. 超过经理收入的员工

LeetCode数据库SQL题目记录(难度:简单)_第3张图片

# Write your MySQL query statement below
select a.Name Employee  
from Employee a,Employee b 
where a.ManagerId = b.Id 
  and a.Salary > b.Salary 

 182. 查找重复的电子邮箱

LeetCode数据库SQL题目记录(难度:简单)_第4张图片

# Write your MySQL query statement below
select Email
from Person 
group by Email   
having count(*) > 1

183. 从不订购的客户

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

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customers-who-never-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select a.Name Customers   
from Customers a 
where not exists(select * from Orders b where a.Id = b.CustomerId)

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

提示:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-duplicate-emails
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
delete from Person 
where Email in (select * from (select Email from Person group by Email having count(*) > 1) b)
  and Id not in (select * from (select min(Id) as id from Person group by Email having count(*) > 1) a)

197. 上升的温度

表 Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id 是这个表的主键
该表包含特定日期的温度信息
 

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

返回结果 不要求顺序 。

查询结果格式如下例:

Weather
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+

Result table:
+----+
| id |
+----+
| 2  |
| 4  |
+----+
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rising-temperature
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select b.id
from Weather a,Weather b 
where TIMESTAMPDIFF(day,a.recordDate,b.recordDate) = 1
  and a.Temperature < b.Temperature 

511. 游戏玩法分析 I

活动表 Activity:

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
表的主键是 (player_id, event_date)。
这张表展示了一些游戏玩家在游戏平台上的行为活动。
每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。
 

写一条 SQL 查询语句获取每位玩家 第一次登陆平台的日期。

查询结果的格式如下所示:

Activity 表:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result 表:
+-----------+-------------+
| player_id | first_login |
+-----------+-------------+
| 1         | 2016-03-01  |
| 2         | 2017-06-25  |
| 3         | 2016-03-02  |
+-----------+-------------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/game-play-analysis-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select player_id,min(event_date) first_login 
from Activity 
group by player_id 

512. 游戏玩法分析 II

Table: Activity

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
(player_id, event_date) 是这个表的两个主键
这个表显示的是某些游戏玩家的游戏活动情况
每一行是在某天使用某个设备登出之前登录并玩多个游戏(可能为0)的玩家的记录
请编写一个 SQL 查询,描述每一个玩家首次登陆的设备名称

查询结果格式在以下示例中:

Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result table:
+-----------+-----------+
| player_id | device_id |
+-----------+-----------+
| 1         | 2         |
| 2         | 3         |
| 3         | 1         |
+-----------+-----------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/game-play-analysis-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select b.player_id,b.device_id from 
(select player_id,min(event_date) first_login 
from Activity 
group by player_id) a,Activity b
where a.player_id = b.player_id
  and a.first_login = b.event_date 

577. 员工奖金

选出所有 bonus < 1000 的员工的 name 及其 bonus。

Employee 表单

+-------+--------+-----------+--------+
| empId |  name  | supervisor| salary |
+-------+--------+-----------+--------+
|   1   | John   |  3        | 1000   |
|   2   | Dan    |  3        | 2000   |
|   3   | Brad   |  null     | 4000   |
|   4   | Thomas |  3        | 4000   |
+-------+--------+-----------+--------+
empId 是这张表单的主关键字
Bonus 表单

+-------+-------+
| empId | bonus |
+-------+-------+
| 2     | 500   |
| 4     | 2000  |
+-------+-------+
empId 是这张表单的主关键字
输出示例:

+-------+-------+
| name  | bonus |
+-------+-------+
| John  | null  |
| Dan   | 500   |
| Brad  | null  |
+-------+-------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/employee-bonus
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select c.name,c.bonus from 
(select a.name,b.bonus
from Employee a left join Bonus b
on a.empId = b.empId) c
where coalesce(c.bonus,0) < 1000

584. 寻找用户推荐人

给定表 customer ,里面保存了所有客户信息和他们的推荐人。

+------+------+-----------+
| id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |
+------+------+-----------+
写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。

对于上面的示例数据,结果为:

+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-customer-referee
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select name from customer where coalesce(referee_id,0) <> 2

586. 订单最多的客户

在表 orders 中找到订单数最多客户对应的 customer_number 。

数据保证订单数最多的顾客恰好只有一位。

表 orders 定义如下:

| Column            | Type      |
|-------------------|-----------|
| order_number (PK) | int       |
| customer_number   | int       |
| order_date        | date      |
| required_date     | date      |
| shipped_date      | date      |
| status            | char(15)  |
| comment           | char(200) |
样例输入

| order_number | customer_number | order_date | required_date | shipped_date | status | comment |
|--------------|-----------------|------------|---------------|--------------|--------|---------|
| 1            | 1               | 2017-04-09 | 2017-04-13    | 2017-04-12   | Closed |         |
| 2            | 2               | 2017-04-15 | 2017-04-20    | 2017-04-18   | Closed |         |
| 3            | 3               | 2017-04-16 | 2017-04-25    | 2017-04-20   | Closed |         |
| 4            | 3               | 2017-04-18 | 2017-04-28    | 2017-04-25   | Closed |         |
样例输出

| customer_number |
|-----------------|
| 3               |
解释

customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都要多,因为他们只有一个订单
所以结果是该顾客的 customer_number ,也就是 3 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customer-placing-the-largest-number-of-orders
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select customer_number from
(select customer_number,count(*) cnt from orders group by customer_number) a 
order by cnt desc
limit 1

595. 大的国家

这里有张 World 表

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+
如果一个国家的面积超过 300 万平方公里,或者人口超过 2500 万,那么这个国家就是大国家。

编写一个 SQL 查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/big-countries
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select name,population,area                    
from World 
where area > 3000000
   or population > 25000000        

596. 超过5名学生的课

有一个courses 表 ,有: student (学生) 和 class (课程)。

请列出所有超过或等于5名学生的课。

例如,表:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+
应该输出:

+---------+
| class   |
+---------+
| Math    |
+---------+
 

提示:

学生在每个课中不应被重复计算。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/classes-more-than-5-students
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select class from courses group by class having count(distinct student) >= 5       

597. 好友申请 I:总体通过率

在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。

表:FriendRequest

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| sender_id      | int     |
| send_to_id     | int     |
| request_date   | date    |
+----------------+---------+
此表没有主键,它可能包含重复项。
该表包含发送请求的用户的 ID ,接受请求的用户的 ID 以及请求的日期。
表:RequestAccepted

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| requester_id   | int     |
| accepter_id    | int     |
| accept_date    | date    |
+----------------+---------+
此表没有主键,它可能包含重复项。
该表包含发送请求的用户的 ID ,接受请求的用户的 ID 以及请求通过的日期。
 

写一个查询语句,求出好友申请的通过率,用 2 位小数表示。通过率由接受好友申请的数目除以申请总数。

提示:

通过的好友申请不一定都在表 friend_request 中。你只需要统计总的被通过的申请数(不管它们在不在表 FriendRequest 中),并将它除以申请总数,得到通过率
一个好友申请发送者有可能会给接受者发几条好友申请,也有可能一个好友申请会被通过好几次。这种情况下,重复的好友申请只统计一次。
如果一个好友申请都没有,通过率为 0.00 。
 

查询结果应该如下例所示:

FriendRequest 表:
+-----------+------------+--------------+
| sender_id | send_to_id | request_date |
+-----------+------------+--------------+
| 1         | 2          | 2016/06/01   |
| 1         | 3          | 2016/06/01   |
| 1         | 4          | 2016/06/01   |
| 2         | 3          | 2016/06/02   |
| 3         | 4          | 2016/06/09   |
+-----------+------------+--------------+

RequestAccepted 表:
+--------------+-------------+-------------+
| requester_id | accepter_id | accept_date |
+--------------+-------------+-------------+
| 1            | 2           | 2016/06/03  |
| 1            | 3           | 2016/06/08  |
| 2            | 3           | 2016/06/08  |
| 3            | 4           | 2016/06/09  |
| 3            | 4           | 2016/06/10  |
+--------------+-------------+-------------+

Result 表:
+-------------+
| accept_rate |
+-------------+
| 0.8         |
+-------------+
总共有 5 个请求,有 4 个不同的通过请求,所以通过率是 0.80

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/friend-requests-i-overall-acceptance-rate
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select round(coalesce(count(distinct a.requester_id,a.accepter_id)/(select count(distinct b.sender_id,b.send_to_id) from FriendRequest b),0),2) accept_rate from RequestAccepted a

603. 连续空余座位

几个朋友来到电影院的售票处,准备预约连续空余座位。

你能利用表 cinema ,帮他们写一个查询语句,获取所有空余座位,并将它们按照 seat_id 排序后返回吗?

| seat_id | free |
|---------|------|
| 1       | 1    |
| 2       | 0    |
| 3       | 1    |
| 4       | 1    |
| 5       | 1    |
 

对于如上样例,你的查询语句应该返回如下结果。

| seat_id |
|---------|
| 3       |
| 4       |
| 5       |
注意:

seat_id 字段是一个自增的整数,free 字段是布尔类型('1' 表示空余, '0' 表示已被占据)。
连续空余座位的定义是大于等于 2 个连续空余的座位。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-available-seats
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
select * from 
(select a.seat_id 
from cinema a,cinema b
where a.seat_id = b.seat_id - 1
  and a.free = 1
  and b.free = 1
union
select a.seat_id 
from cinema a,cinema b
where a.seat_id = b.seat_id + 1
  and a.free = 1
  and b.free = 1) c
order by seat_id

607. 销售员

描述

给定 3 个表: salesperson, company, orders。
输出所有表 salesperson 中,没有向公司 'RED' 销售任何东西的销售员。

示例:
输入

表: salesperson

+----------+------+--------+-----------------+-----------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+-----------+
|   1      | John | 100000 |     6           | 4/1/2006  |
|   2      | Amy  | 120000 |     5           | 5/1/2010  |
|   3      | Mark | 65000  |     12          | 12/25/2008|
|   4      | Pam  | 25000  |     25          | 1/1/2005  |
|   5      | Alex | 50000  |     10          | 2/3/2007  |
+----------+------+--------+-----------------+-----------+
表 salesperson 存储了所有销售员的信息。每个销售员都有一个销售员编号 sales_id 和他的名字 name 。

表: company

+---------+--------+------------+
| com_id  |  name  |    city    |
+---------+--------+------------+
|   1     |  RED   |   Boston   |
|   2     | ORANGE |   New York |
|   3     | YELLOW |   Boston   |
|   4     | GREEN  |   Austin   |
+---------+--------+------------+
表 company 存储了所有公司的信息。每个公司都有一个公司编号 com_id 和它的名字 name 。

表: orders

+----------+------------+---------+----------+--------+
| order_id | order_dat

你可能感兴趣的:(SQL,leetcode,数据库,sql,mysql)