当前位置: 首页 > news >正文

「SQL面试题库」 No_121 The Most Recent Three Orders

🍅 1、专栏介绍

「SQL面试题库」是由 不是西红柿 发起,全员免费参与的SQL学习活动。我每天发布1道SQL面试真题,从简单到困难,涵盖所有SQL知识点,我敢保证只要做完这100道题,不仅能轻松搞定面试,代码能力和工作效率也会有明显提升。

1.1 活动流程

  1. 整理题目:西红柿每天无论刮风下雨,保证在8am 前,更新一道新鲜SQL面试真题。
  2. 粉丝打卡:粉丝们可在评论区写上解题思路,或者直接完成SQL代码,有困难的小伙伴不要着急,先看别人是怎么解题的,边看边学,不懂就问我。
  3. 交流讨论:为了方便交流讨论,可进入 数据仓库
  4. 活动奖励:我每天都会看评论区和群里的内容,对于积极学习和热心解答问题的小伙伴,红包鼓励,以营造更好的学习氛围。

1.2 你的收获

  1. 增强自信,搞定面试:在求职中,SQL是经常遇到的技能点,而这些题目也多数是真实的面试题,刷题可以让我们更好地备战面试,增强自信,提升自己的核心竞争力。

  2. 巩固SQL语法,高效搞定工作:通过不断练习,能够熟悉SQL的语法和常用函数,掌握SQL核心知识点,提高SQL编写能力。代码能力提升了,工作效率自然高了。

  3. 提高数据处理能力、锻炼思维能力:SQL是数据处理的核心工具,通过刷题可以让我们更好地理解数据处理的过程,提高数据分析的效率。SQL题目的难度不一,需要在一定时间内解决问题,培养了我们对问题的思考能力、解决问题的能力和对时间的把控能力等。

🍅 2、今日真题

题目介绍: The Most Recent Three Orders the-most-recent-three-orders

难度中等

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 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. ```

Follow-up: Can you write a general solution for the most recent

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

  • 已经有灵感了?在评论区写下你的思路吧!
http://www.lryc.cn/news/94685.html

相关文章:

  • 【计算机视觉 | 目标检测 | 图像分割】arxiv 计算机视觉关于目标检测和图像分割的学术速递(7 月 7 日论文合集)
  • 直流运算放大器-----仪表放大器(三)
  • 【Zookeeper】终端操作常用命令
  • leetcode 1110. 删点成林
  • 华为Harmony应用开发初探
  • 电脑应用程序发生异常怎么办?
  • 【JAVA】准备工作------Java开发环境搭建,IDEA的基础设置与操作
  • 操作系统真象还原——第5章 保护模式进阶,向内核迈进
  • 设计一款助听器可能需要用到以下音频算法
  • 【端午节】用Vue3写粽子——从零开始
  • 大象机器人人工智能套装2023版深度学习协作机器人、先进机器视觉与应用场景
  • Cesium Token申请
  • ubuntu系统自带的Text Editor编辑器不高亮解决办法
  • Docker NGINX 加载Geoip模板
  • springboot基于协同过滤算法商品推荐系统
  • 基于机器学习算法:朴素贝叶斯和SVM 分类-垃圾邮件识别分类系统(含Python工程全源码)
  • 在Linux下将PNG和JPG批量互转的四种方法
  • Scala中使用 break 和 continue
  • 【全栈开发指南】打包sentinel-dashboard镜像推送到Docker Hub镜像仓库
  • 【数据可视化】SVG(一)
  • linux 系统errno 对应参考及代码
  • PowerShell快速ssh
  • 从php5.6到golang1.19-文库App性能跃迁之路
  • 成功解决 AttributeError: ‘Field‘ object has no attribute ‘vocab‘
  • ikbc键盘2.4G接收器丢失,重新对码
  • STM32 Proteus仿真医用仓库环境控制系统紫外线消毒RS232上传CO2 -0066
  • Docker(二)之容器技术所涉及Linux内核关键技术
  • 计算机网络_ 1.3 网络核心 (数据交换_电路交换)
  • Kafka高性能集群部署与优化
  • Lucene介绍与入门使用