【LeetCode-SQL专项突破】-第5天:合并

简介: 【LeetCode-SQL专项突破】-第5天:合并

8d2b4f9fe7a34aa4914d035e04341dbc.png

175. 组合两个表

faa408e43d3b48b3bd8d9188be06336a.png

🚀 表: Person
+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
personId 是该表的主键列。
该表包含一些人的 ID 和他们的姓和名的信息。
表: Address
+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
addressId 是该表的主键列。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
🚀 需求
编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空  null 。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 1:
输入: 
Person表:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1        | Wang     | Allen     |
| 2        | Alice    | Bob       |
+----------+----------+-----------+
Address表:
+-----------+----------+---------------+------------+
| addressId | personId | city          | state      |
+-----------+----------+---------------+------------+
| 1         | 2        | New York City | New York   |
| 2         | 3        | Leetcode      | California |
+-----------+----------+---------------+------------+
输出: 
+-----------+----------+---------------+----------+
| firstName | lastName | city          | state    |
+-----------+----------+---------------+----------+
| Allen     | Wang     | Null          | Null     |
| Bob       | Alice    | New York City | New York |
+-----------+----------+---------------+----------+
解释: 
地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
addressId = 1 包含了 personId = 2 的地址信息。
🐴🐴 答案
# Write your MySQL query statement below
select 
a.firstName,
a.lastName,
b.city,
b.state
from Person a left join Address b
on a.PersonId = b.PersonId
/* Write your T-SQL query statement below */
select 
a.firstName,
a.lastName,
b.city,
b.state
from Person a left join Address b
on a.PersonId = b.PersonId
/* Write your PL/SQL query statement below */
select 
a.firstName "firstName",
a.lastName "lastName",
b.city "city",
b.state "state"
from Person a,Address b
where a.PersonId = b.PersonId(+)
order by 1


faa408e43d3b48b3bd8d9188be06336a.png


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


🚀 表: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 期间进行的交易的信息。
🚀 需求
有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个 SQL 查询,来查找这些顾客的 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 也两次访问了购物中心并且没有进行任何交易。
🐴🐴 答案
# Write your MySQL query statement below
select 
customer_id "customer_id",
count(*) "count_no_trans"
 from Visits a
where not exists (select * from Transactions b where a.visit_id  = b.visit_id )
group by customer_id
order by 2 desc
/* Write your T-SQL query statement below */
select 
customer_id,
count(*) count_no_trans
 from Visits a
where not exists (select * from Transactions b where a.visit_id  = b.visit_id )
group by customer_id
order by count_no_trans desc
/* Write your PL/SQL query statement below */
select 
customer_id "customer_id",
count(*) "count_no_trans"
 from Visits a
where not exists (select * from Transactions b where a.visit_id  = b.visit_id )
group by customer_id
order by 2 desc

2a603915666740b1854f335e590cd365.png

1148.文章浏览I

11006f979c444602954cd658b3adb2ce.png

🚀 Views 表:
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| article_id    | int     |
| author_id     | int     |
| viewer_id     | int     |
| view_date     | date    |
+---------------+---------+
此表无主键,因此可能会存在重复行。
此表的每一行都表示某人在某天浏览了某位作者的某篇文章。
请注意,同一人的 author_id 和 viewer_id 是相同的。
🚀 需求
请编写一条 SQL 查询以找出所有浏览过自己文章的作者,结果按照 id 升序排列。
查询结果的格式如下所示:
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    |
+------+
🐴🐴 答案
# Write your MySQL query statement below
select distinct author_id id
from Views
where author_id = viewer_id
order by id
/* Write your T-SQL query statement below */
select distinct author_id id
from Views
where author_id = viewer_id
order by id
/* Write your PL/SQL query statement below */
select distinct author_id "id"
from Views
where author_id = viewer_id
order by 1


11006f979c444602954cd658b3adb2ce.png

目录
相关文章
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练(二)
面试必备杀技:SQL查询专项训练
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练(一)
面试必备杀技:SQL查询专项训练
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练
面试必备杀技:SQL查询专项训练
leetcode剑指 Offer 专项突击版(051、008、016)
leetcode剑指 Offer 专项突击版(051、008、016)
103 0
leetcode剑指 Offer 专项突击版(23、047、028、036)
leetcode剑指 Offer 专项突击版(23、047、028、036)
|
SQL 测试技术
LeetCode SQL专项练习 (8) 计算函数
LeetCode SQL专项练习 (8) 计算函数
150 0
LeetCode SQL专项练习 (6) 合并&多表查询
LeetCode SQL专项练习 (6) 合并&多表查询
140 0
LeetCode SQL专项练习 (10) 过滤
LeetCode SQL专项练习 (10) 过滤
165 0
LeetCode SQL专项练习 (9) 控制流
LeetCode SQL专项练习 (9) 控制流
144 0
LeetCode SQL专项练习 (7) 计算函数&分组统计
LeetCode SQL专项练习 (7) 计算函数&分组统计
120 0