🍅 1、专栏介绍
「SQL面试题库」是由 不是西红柿 发起,全员免费参与的SQL学习活动。我每天发布1道SQL面试真题,从简单到困难,涵盖所有SQL知识点,我敢保证只要做完这100道题,不仅能轻松搞定面试,代码能力和工作效率也会有明显提升。
1.1 活动流程
- 整理题目:西红柿每天无论刮风下雨,保证在8am 前,更新一道新鲜SQL面试真题。
- 粉丝打卡:粉丝们可在评论区写上解题思路,或者直接完成SQL代码,有困难的小伙伴不要着急,先看别人是怎么解题的,边看边学,不懂就问我。
- 交流讨论:为了方便交流讨论,可进入 数据仓库 。
- 活动奖励:我每天都会看评论区和群里的内容,对于积极学习和热心解答问题的小伙伴,红包鼓励,以营造更好的学习氛围。
1.2 你的收获
-
增强自信,搞定面试:在求职中,SQL是经常遇到的技能点,而这些题目也多数是真实的面试题,刷题可以让我们更好地备战面试,增强自信,提升自己的核心竞争力。
-
巩固SQL语法,高效搞定工作:通过不断练习,能够熟悉SQL的语法和常用函数,掌握SQL核心知识点,提高SQL编写能力。代码能力提升了,工作效率自然高了。
-
提高数据处理能力、锻炼思维能力:SQL是数据处理的核心工具,通过刷题可以让我们更好地理解数据处理的过程,提高数据分析的效率。SQL题目的难度不一,需要在一定时间内解决问题,培养了我们对问题的思考能力、解决问题的能力和对时间的把控能力等。
🍅 2、今日真题
题目介绍: The Most Recent Orders for Each Product the-most-recent-orders-for-each-product
难度中等
SQL架构
Table:
Customers
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| name | varchar |
+---------------+---------+
customer_id is the primary key for this table.
This table contains information about the customers.
Table:
Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| customer_id | int |
| product_id | int |
+---------------+---------+
order_id is the primary key for this table.
This table contains information about the orders made by customer_id.
There will be no product ordered by the same user more than once in one day.
Table:
Products
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| product_name | varchar |
| price | int |
+---------------+---------+
product_id is the primary key for this table.
This table contains information about the Products.
Write an SQL query to find the most recent order(s) of each product.
Return the result table sorted by
product_name
in
ascending order and in case of a tie by the
product_id
in
ascending order. If there still a tie, order them by the
order_id
in
ascending order.
The query result format is in the following example:
``` Customers +-------------+-----------+ | customer_id | name | +-------------+-----------+ | 1 | Winston | | 2 | Jonathan | | 3 | Annabelle | | 4 | Marwan | | 5 | Khaled | +-------------+-----------+
Orders +----------+------------+-------------+------------+ | order_id | order_date | customer_id | product_id | +----------+------------+-------------+------------+ | 1 | 2020-07-31 | 1 | 1 | | 2 | 2020-07-30 | 2 | 2 | | 3 | 2020-08-29 | 3 | 3 | | 4 | 2020-07-29 | 4 | 1 | | 5 | 2020-06-10 | 1 | 2 | | 6 | 2020-08-01 | 2 | 1 | | 7 | 2020-08-01 | 3 | 1 | | 8 | 2020-08-03 | 1 | 2 | | 9 | 2020-08-07 | 2 | 3 | | 10 | 2020-07-15 | 1 | 2 | +----------+------------+-------------+------------+
Products +------------+--------------+-------+ | product_id | product_name | price | +------------+--------------+-------+ | 1 | keyboard | 120 | | 2 | mouse | 80 | | 3 | screen | 600 | | 4 | hard disk | 450 | +------------+--------------+-------+文章来源:https://www.toymoban.com/news/detail-554149.html
Result table: +--------------+------------+----------+------------+ | product_name | product_id | order_id | order_date | +--------------+------------+----------+------------+ | keyboard | 1 | 6 | 2020-08-01 | | keyboard | 1 | 7 | 2020-08-01 | | mouse | 2 | 8 | 2020-08-03 | | screen | 3 | 3 | 2020-08-29 | +--------------+------------+----------+------------+ keyboard's most recent order is in 2020-08-01, it was ordered two times this day. mouse's most recent order is in 2020-08-03, it was ordered only once this day. screen's most recent order is in 2020-08-29, it was ordered only once this day. The hard disk was never ordered and we don't include it in the result table. ```文章来源地址https://www.toymoban.com/news/detail-554149.html
sql
select product_name,product_id,order_id,order_date
from
(
select product_name ,o.product_id ,order_id,order_date ,
rank() over(partition by o.product_id order by order_date desc) rk
from Orders o left join Products p
on o.product_id =p.product_id
)t1
where rk =1
order by product_name,product_id,order_id
- 已经有灵感了?在评论区写下你的思路吧!
到了这里,关于「SQL面试题库」 No_123 The Most Recent Orders for Each Product的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!