SQL_boy的SQL查询长期训练,每日更新3~5题……

  • 每天坚持刷3~5道题,本博客长期更新……
  • 截至今日第19天,已刷并整理图文解题思路的题共计:73
  • 学习:知识的初次邂逅
  • 复习:知识的温故知新
  • 练习:知识的实践应用

目录

一,2024-07-20日训练

第一题:1757,可回收且的低脂的产品(简单)1

第二题:584,寻找用户推荐人(简单)2

第三题:595,大的国家(简单)3

二,2024-07-21日训练

第一题:1148,文章浏览(简单)4

第二题:1683,无效的推文(简单)5

第三题:1378,使用唯一标识码替换员工ID(简单)6

第四题:1086,产品销售分析(简单)7

第五题:1581,进店却从未进行交易的顾客(简单)8

三,2024-7-22日训练

 第一题:262.行程与用户(困难)9

第二题,185,部门工资前三高的所有员工(困难)(经典面试题 分组求TopN)10

第三题:197,上升的温度(自连接和窗口函数分别解答)(简单)11

四,2024-7-23日训练 

第一题:1661,每天机器的进程的平均运行时间(自连接和窗口函数分别解答)(简单)12

第二题:577,员工奖金(简单)13

第三题:620,有趣的电影(简单)14

五,2024-7-24日训练

第一题:1280,学生参加各科考试的次数(简单)15

第二题:550,游戏玩法分析IV(经典连续登录天数)(困难)16

第三题:570,至少有5名直接下属的领导(中等17

六,2024-7-25日训练

第一题:最低连续登录3天的人(困难

第二题:最低连续登录3天的人(间隔一天也算连续)(困难18

第三题,1934,确认率(中等19

第四题,569,员工薪水的中位数(困难20

七,2024-7-26日训练

第一题:力扣1251,平均售价(简单)21

第二题: 力扣 1075,项目员工(简单)22

第三题:力扣1633,各赛事的用户注册率(简单)23

第四题:力扣1164,指定日期的产品价格(中等24

第五题:力扣1121查询结果的质量和占比(简单)25

第六题:力扣,1193,每月交易 I(中等26

第七题:力扣,1174,即时食物配送 II(中等27

第八题:力扣,2356,每位教师所教授的科目种类的数量(中等28

第九题:力扣,1141. 查询近30天活跃用户数(简单)29

第十篇:SQL训练之:力扣,1084,销售分析III(简单)30

八,2024-7-27日训练

第一题:SQL训练之:力扣 596. 超过5名学生的课(简单)31

第二题:SQL训练之:力扣,1729. 求关注者的数量(简单)32

第三题:SQL训练之:力扣,619. 只出现一次的最大数字(简单)33

第四题:SQL训练之:力扣,1045. 买下所有产品的客户(中等)34

第五题:SQL训练之:力扣,176.第二高的薪水(中等)35

,2024-7-28日训练

第一题:SQL训练之:力扣,1731. 每位经理的下属员工数量(简单)36

第二题:SQL训练之,力扣,177. 第N高的薪水(中等37

第三题:SQL训练之,力扣,1789. 员工的直属部门(简单)38

十,2024-7-29日训练

第一题:SQL训练之:力扣,610,判断三角形(简单)39

第二题:SQL训练之:力扣,180,连续出现的数字(中等40

第三题:SQL训练之:力扣,1204. 最后一个能进入巴士的人。经典面试题,最后一个上电梯的人(中等)41

十一,2024-7-30日训练

第一题:SQL训练之:力扣,1907. 按分类统计薪水(中等42

第二题:SQL训练之:力扣,1978. 上级经理已离职的公司员工(简单)43

十二,2024-7-31日训练

第一题:SQL训练之,力扣,626,换座位(中等44

第二题:SQL训练之,力扣,1341,电影评分(中等45

第三题:SQL训练之,力扣,1321,餐馆营业额变化增长(中等46

十三,2024-8-1日训练

第一题:SQL训练之,602. 好友申请 II :谁有最多的好友(中等47

第二题:SQL训练之,力扣,585. 2016年的投资(中等48

第三题:SQL训练之,力扣,1667. 修复表中的名字(简单)49

十四,2024-8-2日训练

第一题:SQL训练之,力扣,1527. 患某种疾病的患者(简单)50

第二题:SQL训练之,力扣,196. 删除重复的电子邮箱(简单)51

第三题:SQL训练之,力扣,1484. 按日期分组销售产品(简单)52

第四题:SQL训练之,力扣,1327. 列出指定时间段内所有的下单产品(简单)53

第五题:SQL训练之,力扣,1517. 查找拥有有效邮箱的用户(简单)54

第六题:SQL训练之,力扣,1821. 寻找今年具有正收入的客户(简单)55

第七题:SQL训练之,力扣,183. 从不订购的客户(简单)56

第八题:SQL训练之,力扣,1873. 计算特殊奖金(简单)57

第九题:SQL训练之,力扣,1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客(中等58

第十题:SQL训练之,力扣,1112. 每位学生的最高成绩(中等59

第十一题:SQL训练之,力扣,175. 组合两个表(简单)60

第十二题:SQL训练之,力扣,1607. 没有卖出的卖家(简单)61

第十三题:SQL训练之,力扣,1407. 排名靠前的旅行者(简单)62

第十四题:SQL训练之,力扣,607. 销售员(简单)63

第十五题:SQL训练之,力扣,1440. 计算布尔表达式的值(简单)64

十五,2024-8-3日训练

第一题:SQL训练之,力扣,1212. 查询球队积分(中等65

第二题:SQL训练之,力扣,1890. 2020年最后一次登录(简单)66

十六,2024-8-4日训练

第一题:SQL训练之,力扣,511. 游戏玩法分析 I(简单)67

第二题:SQL训练之,力扣,1571. 仓库经理(简单)68


十七,2024-8-5日训练

第一题:60,SQL训练之,力扣,586,订单最多的客户(简单)69

第二题:61,SQL训练之,1741. 查找每个员工花费的总时间(简单)70

十八,2024-8-6日训练

第一题:62,SQL训练之,力扣,1173. 即时食物配送 I(简单)71

第一题:63,SQL训练之,1445. 苹果和桔子(中等72

十九,2024-8-7日训练

第一题:SQL训练之,力扣,1699. 两人之间的通话次数(中等




一,2024-07-20日训练

第一题:1757,可回收且的低脂的产品(简单)

1757.1,力扣原题链接

. - 力扣(LeetCode)

1757.2,题干 :

/*
编写解决方案找出既是低脂又是可回收的产品编号。

返回结果 无顺序要求 。

返回结果格式如下例所示:
示例 1:
输入:
Products 表:
+-------------+----------+------------+
| product_id  | low_fats | recyclable |
+-------------+----------+------------+
| 0           | Y        | N          |
| 1           | Y        | Y          |
| 2           | N        | Y          |
| 3           | Y        | Y          |
| 4           | N        | N          |
+-------------+----------+------------+
输出:
+-------------+
| product_id  |
+-------------+
| 1           |
| 3           |
+-------------+
解释:
只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。

1757.3,建表语句:

Create table If Not Exists Products (product_id int, low_fats ENUM('Y', 'N'), recyclable ENUM('Y','N'))
Truncate table Products
insert into Products (product_id, low_fats, recyclable) values ('0', 'Y', 'N')
insert into Products (product_id, low_fats, recyclable) values ('1', 'Y', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('2', 'N', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('3', 'Y', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('4', 'N', 'N')

1757.4,思路

表分析: 字段:产品id,低脂,可回收

要求字段: 产品id;

条件:  既是低脂也是可回收   双重要求 需要使用and。

                where low_fats='Y' and recyclable='Y'

最终结果要找到:

1757.5,解答:

select product_id from Products where low_fats='Y' and recyclable='Y';

1757.6,验证: 

力扣网验证

1757.7,知识点:

多条件判定。and 两变都满足才返回true

第二题:584,寻找用户推荐人(简单)

584.1,原题链接:

. - 力扣(LeetCode)

584.2,题干:

表: Customer

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| referee_id  | int     |
+-------------+---------+
在 SQL 中,id 是该表的主键列。
该表的每一行表示一个客户的 id、姓名以及推荐他们的客户的 id。

找出那些 没有被 id = 2 的客户 推荐 的客户的姓名。

以 任意顺序 返回结果表。

结果格式如下所示。

输入: 
Customer 表:
+----+------+------------+
| id | name | referee_id |
+----+------+------------+
| 1  | Will | null       |
| 2  | Jane | null       |
| 3  | Alex | 2          |
| 4  | Bill | null       |
| 5  | Zack | 1          |
| 6  | Mark | 2          |
+----+------+------------+
输出:
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

584.3,创表语句:

Create table If Not Exists Customer (id int, name varchar(25), referee_id int)
Truncate table Customer
insert into Customer (id, name, referee_id) values ('1', 'Will', 'None')
insert into Customer (id, name, referee_id) values ('2', 'Jane', 'None')
insert into Customer (id, name, referee_id) values ('3', 'Alex', '2')
insert into Customer (id, name, referee_id) values ('4', 'Bill', 'None')
insert into Customer (id, name, referee_id) values ('5', 'Zack', '1')
insert into Customer (id, name, referee_id) values ('6', 'Mark', '2')

584.4,思路

  1. 表字段名: id,名称,和推荐人的客户
  2. 要返回的字段 :name
  3. 条件:referee_id 不等于2  空值也包括  where referee!=2 or referee_id is null;
  4. SQL语句结构:单表 双条件

 584.5,解题:

select name from Customer where referee_id !=2 or referee_id is null;

584.6,验证: 

网站验证 

584.7,知识点

多条件筛选,or,任何一方满足都返回true;


第三题:595,大的国家(简单)

595.1,原题连接

. - 力扣(LeetCode)

595.2,题干

World 表:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| name        | varchar |
| continent   | varchar |
| area        | int     |
| population  | int     |
| gdp         | bigint  |
+-------------+---------+
name 是该表的主键(具有唯一值的列)。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。

如果一个国家满足下述两个条件之一,则认为该国是 大国 :

  • 面积至少为 300 万平方公里(即,3000000 km2),或者
  • 人口至少为 2500 万(即 25000000

编写解决方案找出 大国 的国家名称、人口和面积。

按 任意顺序 返回结果表。

返回结果格式如下例所示。

示例:

输入:
World 表:
+-------------+-----------+---------+------------+--------------+
| name        | continent | area    | population | gdp          |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia      | 652230  | 25500100   | 20343000000  |
| Albania     | Europe    | 28748   | 2831741    | 12960000000  |
| Algeria     | Africa    | 2381741 | 37100000   | 188681000000 |
| Andorra     | Europe    | 468     | 78115      | 3712000000   |
| Angola      | Africa    | 1246700 | 20609294   | 100990000000 |
+-------------+-----------+---------+------------+--------------+
输出:
+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan | 25500100   | 652230  |
| Algeria     | 37100000   | 2381741 |
+-------------+------------+---------+

595.3,建表语句

Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp bigint)
Truncate table World
insert into World (name, continent, area, population, gdp) values ('Afghanistan', 'Asia', '652230', '25500100', '20343000000')
insert into World (name, continent, area, population, gdp) values ('Albania', 'Europe', '28748', '2831741', '12960000000')
insert into World (name, continent, area, population, gdp) values ('Algeria', 'Africa', '2381741', '37100000', '188681000000')
insert into World (name, continent, area, population, gdp) values ('Andorra', 'Europe', '468', '78115', '3712000000')
insert into World (name, continent, area, population, gdp) values ('Angola', 'Africa', '1246700', '20609294', '100990000000')

595.4,分析

1,要输出的字段:name,population,area   他们都来自world表。单表查询

2,需要条件1:面积(area) >=3000000

3,需要的条件2:人口(population)>= 25000000

4,条件的关系:或者 or

5,结构:单表  双条件 查询

595.5,sql题目

select name,population,area
 from World 
where area >= 3000000  or population >=25000000;

验证: 

官网验证:

6,知识点总结:

双条件查询,或的关系。


二,2024-07-21日训练


第一题:1148,文章浏览(简单)

1148.1,原题连接

. - 力扣(LeetCode)

1148.2,题干:

Views 表:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| article_id    | int     |
| author_id     | int     |
| viewer_id     | int     |
| view_date     | date    |
+---------------+---------+
此表可能会存在重复行。(换句话说,在 SQL 中这个表没有主键)
此表的每一行都表示某人在某天浏览了某位作者的某篇文章。
请注意,同一人的 author_id 和 viewer_id 是相同的。

请查询出所有浏览过自己文章的作者

结果按照 id 升序排列。

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

示例 1:

输入:
Views 表:
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date  |
+------------+-----------+-----------+------------+
| 1          | 3         | 5         | 2019-08-01 |
| 1          | 3         | 6         | 2019-08-02 |
| 2          | 7         | 7         | 2019-08-01 |
| 2          | 7         | 6         | 2019-08-02 |
| 4          | 7         | 1         | 2019-07-22 |
| 3          | 4         | 4         | 2019-07-21 |
| 3          | 4         | 4         | 2019-07-21 |
+------------+-----------+-----------+------------+

输出:
+------+
| id   |
+------+
| 4    |
| 7    |
+------+

1148.3,建表语句

Create table If Not Exists Views (article_id int, author_id int, viewer_id int, view_date date)
Truncate table Views
insert into Views (article_id, author_id, viewer_id, view_date) values ('1', '3', '5', '2019-08-01')
insert into Views (article_id, author_id, viewer_id, view_date) values ('1', '3', '6', '2019-08-02')
insert into Views (article_id, author_id, viewer_id, view_date) values ('2', '7', '7', '2019-08-01')
insert into Views (article_id, author_id, viewer_id, view_date) values ('2', '7', '6', '2019-08-02')
insert into Views (article_id, author_id, viewer_id, view_date) values ('4', '7', '1', '2019-07-22')
insert into Views (article_id, author_id, viewer_id, view_date) values ('3', '4', '4', '2019-07-21')
insert into Views (article_id, author_id, viewer_id, view_date) values ('3', '4', '4', '2019-07-21')

1148.4,分析

  1. 原表字段:文章id,作者di,读者id,查看时间
  2. 题目要求查询的字段:作者id  
  3. 该字段来源自表:views表 
  4. 条件1:读者id和作者id是相同的,也就是viewer_id=author_id
  5. 条件2:按照作者id排序  显示的字段名是id ,那么还需要给取一个别名
  6. sql结构:单表查询,单条件语句

1148.5,sql

select distinct author_id as id  from Views
where author_id = viewer_id 
order by id;

1148.6,验证


第二题:1683,无效的推文(简单)

1683.1,原题链接

. - 力扣(LeetCode)

1683.2,题干:

表:Tweets

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| tweet_id       | int     |
| content        | varchar |
+----------------+---------+
在 SQL 中,tweet_id 是这个表的主键。
这个表包含某社交媒体 App 中所有的推文。

查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15 时,该推文是无效的。

任意顺序返回结果表。

查询结果格式如下所示:

示例 1:

输入:
Tweets 表:
+----------+----------------------------------+
| tweet_id | content                          |
+----------+----------------------------------+
| 1        | Vote for Biden                   |
| 2        | Let us make America great again! |
+----------+----------------------------------+

输出:
+----------+
| tweet_id |
+----------+
| 2        |
+----------+
解释:
推文 1 的长度 length = 14。该推文是有效的。
推文 2 的长度 length = 32。该推文是无效的。

1683.3,建表语句

Create table If Not Exists Tweets(tweet_id int, content varchar(50))
Truncate table Tweets
insert into Tweets (tweet_id, content) values ('1', 'Vote for Biden')
insert into Tweets (tweet_id, content) values ('2', 'Let us make America great again!')

1683.4,分析

  1. 需要查询的字段:编号id  tweet_id
  2. 该字段来源自:Tweets表,单表查询
  3. 查询需要的条件:推文内容的字符严格大于15的时。 那就是content的长度 >15
  4. 求内容长度,用到的函数有CHAR_LENGTH(字段).CHAR_LENGTH(content) > 15;
  5. sql结构:单表查询,单条件判定

1683.5,SQL查询语句

select tweet_id from Tweets where CHAR_LENGTH(content) > 15;

1683.6,验证:

1683.7,知识点总结

  • 单表查询,单条件判定;
  • 查询字段长度的函数 char_length()的运用;


第三题:1378,使用唯一标识码替换员工ID(简单)

1378.1,原题链接

. - 力扣(LeetCode)

1378.2,题干:

Employees 表:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
在 SQL 中,id 是这张表的主键。
这张表的每一行分别代表了某公司其中一位员工的名字和 ID 。

EmployeeUNI 表:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| unique_id     | int     |
+---------------+---------+
在 SQL 中,(id, unique_id) 是这张表的主键。
这张表的每一行包含了该公司某位员工的 ID 和他的唯一标识码(unique ID)。

展示每位用户的 唯一标识码(unique ID );如果某位员工没有唯一标识码,使用 null 填充即可。

你可以以 任意 顺序返回结果表。

返回结果的格式如下例所示。

示例 1:

输入:
Employees 表:
+----+----------+
| id | name     |
+----+----------+
| 1  | Alice    |
| 7  | Bob      |
| 11 | Meir     |
| 90 | Winston  |
| 3  | Jonathan |
+----+----------+
EmployeeUNI 表:
+----+-----------+
| id | unique_id |
+----+-----------+
| 3  | 1         |
| 11 | 2         |
| 90 | 3         |
+----+-----------+
输出:
+-----------+----------+
| unique_id | name     |
+-----------+----------+
| null      | Alice    |
| null      | Bob      |
| 2         | Meir     |
| 3         | Winston  |
| 1         | Jonathan |
+-----------+----------+
解释:
Alice and Bob 没有唯一标识码, 因此我们使用 null 替代。
Meir 的唯一标识码是 2 。
Winston 的唯一标识码是 3 。
Jonathan 唯一标识码是 1 。

1378.3,建表语句

Create table If Not Exists Employees (id int, name varchar(20))
Create table If Not Exists EmployeeUNI (id int, unique_id int)
Truncate table Employees
insert into Employees (id, name) values ('1', 'Alice')
insert into Employees (id, name) values ('7', 'Bob')
insert into Employees (id, name) values ('11', 'Meir')
insert into Employees (id, name) values ('90', 'Winston')
insert into Employees (id, name) values ('3', 'Jonathan')
Truncate table EmployeeUNI
insert into EmployeeUNI (id, unique_id) values ('3', '1')
insert into EmployeeUNI (id, unique_id) values ('11', '2')
insert into EmployeeUNI (id, unique_id) values ('90', '3')

1378.4,分析

1。需要查询的字段:用户的唯一标识码 id  ;员工的姓名 name

2,来源的表:唯一标识码的id 来源 employeeUNI表; 员工的姓名 来源自employees表;

3,查询的条件:如果员工没有唯一的标识码,则用null来填充

4,思考:

  • 解法一:用左连接的思路 最优解
  • 要查询的字段来源两张表,所以需要连接查询。
  • 没有员工的id 用null来填充,那么可以用左连接来查询。 
  • 左表employees表  连接  右表 employeeNUI  连接条件 两个表的id相同
  • 解法二:用子查询的思路 
  • 子查询需要放置再查询的字段上,
  • 子查询的条件是 唯一标识符表的id等于员工表的id
  • 当条件不满足的时候,而第二列name还有值,所以它也返回null

5,SQL结构:双表查询,左连接

1378.5,SQL语句

select e2.unique_id,e1.name  from
Employees e1
 left join EmployeeUNI e2 
on e2.id =e1.id;

解法二,子查询

SELECT  
    (SELECT e2.unique_id FROM EmployeeUNI e2 WHERE e2.id = e1.id) AS unique_id,  
    e1.name  
FROM  
    Employees e1;

1378.6,验证

解法一:

解法二:

1378.7,知识点总结

  • 本题最优解法用的是连接查询,左连接。
  • 左连接:查询左表的全部数据,以及左表和右表都包括的数据。当右表不满足的时候以null填充;
  • 解法二:使用的子查询。
  • 子查询的查询性能比连接要欠缺,故优先选择 连接查询;
  • 子查询语句:直接字段开始子查询,然后再用连接


第四题:1086,产品销售分析(简单)

1086.1,原题连接

. - 力扣(LeetCode)

1086.2,题干:

销售表 Sales

+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| sale_id     | int   |
| product_id  | int   |
| year        | int   |
| quantity    | int   |
| price       | int   |
+-------------+-------+
(sale_id, year) 是销售表 Sales 的主键(具有唯一值的列的组合)。
product_id 是关联到产品表 Product 的外键(reference 列)。
该表的每一行显示 product_id 在某一年的销售情况。
注意: price 表示每单位价格。

产品表 Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
+--------------+---------+
product_id 是表的主键(具有唯一值的列)。
该表的每一行表示每种产品的产品名称。

编写解决方案,以获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。

返回结果表 无顺序要求 。

结果格式示例如下。

示例 1:

输入:
Sales 表:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+ 
| 1       | 100        | 2008 | 10       | 5000  |
| 2       | 100        | 2009 | 12       | 5000  |
| 7       | 200        | 2011 | 15       | 9000  |
+---------+------------+------+----------+-------+
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100        | Nokia        |
| 200        | Apple        |
| 300        | Samsung      |
+------------+--------------+
输出:
+--------------+-------+-------+
| product_name | year  | price |
+--------------+-------+-------+
| Nokia        | 2008  | 5000  |
| Nokia        | 2009  | 5000  |
| Apple        | 2011  | 9000  |
+--------------+-------+-------+

1086.3, 建表语句

Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)
Create table If Not Exists Product (product_id int, product_name varchar(10))
Truncate table Sales
insert into Sales (sale_id, product_id, year, quantity, price) values ('1', '100', '2008', '10', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('2', '100', '2009', '12', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('7', '200', '2011', '15', '9000')
Truncate table Product
insert into Product (product_id, product_name) values ('100', 'Nokia')
insert into Product (product_id, product_name) values ('200', 'Apple')
insert into Product (product_id, product_name) values ('300', 'Samsung')

1086.4, 分析

  1. 查询要求的字段:产品名称(product_name),年(year),价格(price);
  2. 查询要用到的表1:产品表 product中的产品名称;
  3. 查询要用到的表2:销售表(Sales)中的 年(year),价格(price)
  4. 条件:直接要字段:销售表中的id对应的产品名称,和产品表中的年和价格
  5. 分析:
  6. 思路一:用连接。2表连接,然后拿到想要的字段即可
  7. 思路二:用子查询。也是要连接;

1086.5, SQL语句

解法一: 用内连接

select p.product_name,s.year,s.price 
from Sales s ,Product p 
where s.product_id=p.product_id;

解法二:用子查询

select t2.product_name as product_name , year,price 
from Sales ,(select product_id,product_name from Product) t2
 where t2.product_id=Sales.product_id;

1086.6, 提交验证

解法一验证:

解法二验证:

1086.7, 知识点总结

  • 两表内连接的练习;
  • 内连接两个表,取共同的数据,然后查找想要的字段
  • 子查询,提取查询一个表,拿到字段,然后和主表连接。最终拿到想要的字段;
  • 推荐内连接,子查询性能不高;


第五题:1581,进店却从未进行交易的顾客(简单)

1581.1,原题链接

. - 力扣(LeetCode)

1581.2,题干:

表:Visits

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| visit_id    | int     |
| customer_id | int     |
+-------------+---------+
visit_id 是该表中具有唯一值的列。
该表包含有关光临过购物中心的顾客的信息。

表:Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| transaction_id | int     |
| visit_id       | int     |
| amount         | int     |
+----------------+---------+
transaction_id 是该表中具有唯一值的列。
此表包含 visit_id 期间进行的交易的信息。

有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个解决方案,来查找这些顾客的 ID ,以及他们只光顾不交易的次数。

返回以 任何顺序 排序的结果表。

返回结果格式如下例所示。

示例 1:

输入:
Visits
+----------+-------------+
| visit_id | customer_id |
+----------+-------------+
| 1        | 23          |
| 2        | 9           |
| 4        | 30          |
| 5        | 54          |
| 6        | 96          |
| 7        | 54          |
| 8        | 54          |
+----------+-------------+
Transactions
+----------------+----------+--------+
| transaction_id | visit_id | amount |
+----------------+----------+--------+
| 2              | 5        | 310    |
| 3              | 5        | 300    |
| 9              | 5        | 200    |
| 12             | 1        | 910    |
| 13             | 2        | 970    |
+----------------+----------+--------+
输出:
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54          | 2              |
| 30          | 1              |
| 96          | 1              |
+-------------+----------------+
解释:
ID = 23 的顾客曾经逛过一次购物中心,并在 ID = 12 的访问期间进行了一笔交易。
ID = 9 的顾客曾经逛过一次购物中心,并在 ID = 13 的访问期间进行了一笔交易。
ID = 30 的顾客曾经去过购物中心,并且没有进行任何交易。
ID = 54 的顾客三度造访了购物中心。在 2 次访问中,他们没有进行任何交易,在 1 次访问中,他们进行了 3 次交易。
ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。
如我们所见,ID 为 30 和 96 的顾客一次没有进行任何交易就去了购物中心。顾客 54 也两次访问了购物中心并且没有进行任何交易。

1581.3,建表语句:

Create table If Not Exists Visits(visit_id int, customer_id int)
Create table If Not Exists Transactions(transaction_id int, visit_id int, amount int)
Truncate table Visits
insert into Visits (visit_id, customer_id) values ('1', '23')
insert into Visits (visit_id, customer_id) values ('2', '9')
insert into Visits (visit_id, customer_id) values ('4', '30')
insert into Visits (visit_id, customer_id) values ('5', '54')
insert into Visits (visit_id, customer_id) values ('6', '96')
insert into Visits (visit_id, customer_id) values ('7', '54')
insert into Visits (visit_id, customer_id) values ('8', '54')
Truncate table Transactions
insert into Transactions (transaction_id, visit_id, amount) values ('2', '5', '310')
insert into Transactions (transaction_id, visit_id, amount) values ('3', '5', '300')
insert into Transactions (transaction_id, visit_id, amount) values ('9', '5', '200')
insert into Transactions (transaction_id, visit_id, amount) values ('12', '1', '910')
insert into Transactions (transaction_id, visit_id, amount) values ('13', '2', '970')

1581.4,分析

  1. 要查询的字段1:顾客id customer_id,
  2. 要查询的字段2:顾客有访问记录但是没交易记录的次数
  3. 要用到的表:字段1用到访问记录表;字段2需要要用到则2个表拼接之后统计次数
  4. 分析:
  5. 字段2 ,需要连接2个表,然后统计有访问id记录,但是没有交易id记录;需要用左连接;
  6. 分组求聚合 ,以访问id 分组,统计 交易记录是空的 id数量
  7. SQL语句结构:左连接查询,然后分组, 筛选交易记录非空,最后count 统计

1581.5,SQL查询语句

select v.customer_id, count(v.customer_id) as count_no_trans
from Visits v
    LEFT JOIN Transactions t on v.visit_id = t.visit_id
where
    t.transaction_id is null
group by
    v.customer_id;

1581.6,提交验证

1581.7,知识点总结

  1. 左连接查询练习.
  2. 分组+聚合练习
  3. is null 判断练习

三,2024-7-22日训练

 第一题:262.行程与用户(困难)

262.1,原题连接

. - 力扣(LeetCode)

262.2,题干

表:Trips

+-------------+----------+
| Column Name | Type     |
+----
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值