?? 1、專欄介紹
「SQL面試題庫(kù)」是由 不是西紅柿 發(fā)起,全員免費(fèi)參與的SQL學(xué)習(xí)活動(dòng)。我每天發(fā)布1道SQL面試真題,從簡(jiǎn)單到困難,涵蓋所有SQL知識(shí)點(diǎn),我敢保證只要做完這100道題,不僅能輕松搞定面試,代碼能力和工作效率也會(huì)有明顯提升。
1.1 活動(dòng)流程
- 整理題目:西紅柿每天無(wú)論刮風(fēng)下雨,保證在8am 前,更新一道新鮮SQL面試真題。
- 粉絲打卡:粉絲們可在評(píng)論區(qū)寫上解題思路,或者直接完成SQL代碼,有困難的小伙伴不要著急,先看別人是怎么解題的,邊看邊學(xué),不懂就問我。
- 交流討論:為了方便交流討論,可進(jìn)入 數(shù)據(jù)倉(cāng)庫(kù) 。
- 活動(dòng)獎(jiǎng)勵(lì):我每天都會(huì)看評(píng)論區(qū)和群里的內(nèi)容,對(duì)于積極學(xué)習(xí)和熱心解答問題的小伙伴,紅包鼓勵(lì),以營(yíng)造更好的學(xué)習(xí)氛圍。
1.2 你的收獲
-
增強(qiáng)自信,搞定面試:在求職中,SQL是經(jīng)常遇到的技能點(diǎn),而這些題目也多數(shù)是真實(shí)的面試題,刷題可以讓我們更好地備戰(zhàn)面試,增強(qiáng)自信,提升自己的核心競(jìng)爭(zhēng)力。
-
鞏固SQL語(yǔ)法,高效搞定工作:通過不斷練習(xí),能夠熟悉SQL的語(yǔ)法和常用函數(shù),掌握SQL核心知識(shí)點(diǎn),提高SQL編寫能力。代碼能力提升了,工作效率自然高了。
-
提高數(shù)據(jù)處理能力、鍛煉思維能力:SQL是數(shù)據(jù)處理的核心工具,通過刷題可以讓我們更好地理解數(shù)據(jù)處理的過程,提高數(shù)據(jù)分析的效率。SQL題目的難度不一,需要在一定時(shí)間內(nèi)解決問題,培養(yǎng)了我們對(duì)問題的思考能力、解決問題的能力和對(duì)時(shí)間的把控能力等。
?? 2、今日真題
題目介紹: The Most Recent Three Orders the-most-recent-three-orders
難度中等
SQL架構(gòu)
Table:
Customers
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| name | varchar |
+---------------+---------+
customer_id is the primary key for this table.
This table contains information about customers.
Table:
Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| customer_id | int |
| cost | int |
+---------------+---------+
order_id is the primary key for this table.
This table contains information about the orders made by customer_id.
Each customer has one order per day.
Write an SQL query to find the most recent 3 orders of each user. If a user ordered less than 3 orders return all of their orders.
Return the result table sorted by
customer_name
in
ascending order and in case of a tie by the
customer_id
in
ascending order. If there still a tie, order them by the
order_date
in
descending 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 | cost | +----------+------------+-------------+------+ | 1 | 2020-07-31 | 1 | 30 | | 2 | 2020-07-30 | 2 | 40 | | 3 | 2020-07-31 | 3 | 70 | | 4 | 2020-07-29 | 4 | 100 | | 5 | 2020-06-10 | 1 | 1010 | | 6 | 2020-08-01 | 2 | 102 | | 7 | 2020-08-01 | 3 | 111 | | 8 | 2020-08-03 | 1 | 99 | | 9 | 2020-08-07 | 2 | 32 | | 10 | 2020-07-15 | 1 | 2 | +----------+------------+-------------+------+
Result table: +---------------+-------------+----------+------------+ | customer_name | customer_id | order_id | order_date | +---------------+-------------+----------+------------+ | Annabelle | 3 | 7 | 2020-08-01 | | Annabelle | 3 | 3 | 2020-07-31 | | Jonathan | 2 | 9 | 2020-08-07 | | Jonathan | 2 | 6 | 2020-08-01 | | Jonathan | 2 | 2 | 2020-07-30 | | Marwan | 4 | 4 | 2020-07-29 | | Winston | 1 | 8 | 2020-08-03 | | Winston | 1 | 1 | 2020-07-31 | | Winston | 1 | 10 | 2020-07-15 | +---------------+-------------+----------+------------+ Winston has 4 orders, we discard the order of "2020-06-10" because it is the oldest order. Annabelle has only 2 orders, we return them. Jonathan has exactly 3 orders. Marwan ordered only one time. We sort the result table by customer_name in ascending order, by customer_id in ascending order and by order_date in descending order in case of a tie. ```文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-540505.html
Follow-up: Can you write a general solution for the most recent 文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-540505.html
n
orders?
sql
select name customer_name ,customer_id,order_id,order_date
from (
select name ,o.customer_id,order_id,order_date ,rank()over(partition by o.customer_id order by order_date desc) rk
from Orders o left join Customers c
on o.customer_id=c.customer_id
)t1
where rk <=3
order by customer_name ,customer_id,order_date desc
- 已經(jīng)有靈感了?在評(píng)論區(qū)寫下你的思路吧!
到了這里,關(guān)于「SQL面試題庫(kù)」 No_121 The Most Recent Three Orders的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!