「SQL面试题库」 No_122 Fix Product Name Format

简介: 「SQL面试题库」 No_122 Fix Product Name Format

🍅 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、今日真题

题目介绍: Fix Product Name Format fix-product-name-format

难度简单

SQL架构

Table:

Sales
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| sale_id      | int     |
| product_name | varchar |
| sale_date    | date    |
+--------------+---------+
sale_id is the primary key for this table.
Each row of this table contains the product name and the date it was sold.

Since table Sales was filled manually in the year 2000,

product_name

may contain leading and/or trailing white spaces, also they are case-insensitive.

Write an SQL query to report

product_name
  • in lowercase without leading or trailing white spaces.
sale_date
  • in the format
('YYYY-MM')
total
  • the number of times the product was sold in this month.

Return the result table ordered by

product_name

in ascending order, in case of a tie order it by

sale_date

in ascending order.

The query result format is in the following example.

``` Sales +------------+------------------+--------------+ | sale_id | product_name | sale_date | +------------+------------------+--------------+ | 1 | LCPHONE | 2000-01-16 | | 2 | LCPhone | 2000-01-17 | | 3 | LcPhOnE | 2000-02-18 | | 4 | LCKeyCHAiN | 2000-02-19 | | 5 | LCKeyChain | 2000-02-28 | | 6 | Matryoshka | 2000-03-31 | +------------+------------------+--------------+

Result table: +--------------+--------------+----------+ | product_name | sale_date | total | +--------------+--------------+----------+ | lcphone | 2000-01 | 2 | | lckeychain | 2000-02 | 2 | | lcphone | 2000-02 | 1 | | matryoshka | 2000-03 | 1 | +--------------+--------------+----------+

In January, 2 LcPhones were sold, please note that the product names are not case sensitive and may contain spaces. In Februery, 2 LCKeychains and 1 LCPhone were sold. In March, 1 matryoshka was sold. ```

sql
select trim(lower(product_name)) as product_name, 
        date_format(sale_date,'%Y-%m') as sale_date,
        count(*) as total 
from Sales 
group by trim(lower(product_name)), date_format(sale_date,'%Y-%m') 
order by product_name asc, sale_date asc

注意大小写、空格

  • 已经有灵感了?在评论区写下你的思路吧!
相关文章
|
1月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
67 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
2月前
|
SQL 存储 数据库
面试题19: 如何优化SQL查询?
面试题19: 如何优化SQL查询?
面试题19: 如何优化SQL查询?
|
1月前
|
存储 关系型数据库 MySQL
最全MySQL面试60题(含答案):存储引擎+数据库锁+索引+SQL优化等
最全MySQL面试60题(含答案):存储引擎+数据库锁+索引+SQL优化等
178 0
|
24天前
|
SQL 关系型数据库 MySQL
SQL常见面试题总结2
SQL常见面试题总结
52 2
|
30天前
|
SQL 关系型数据库 MySQL
MySQL SQL语句面试准备
MySQL SQL语句面试准备
13 0
|
2月前
|
SQL 关系型数据库 MySQL
|
2月前
|
SQL 关系型数据库 MySQL
慢SQL(面试题)
慢SQL(面试题)
31 1
|
3月前
|
SQL
面试必备杀技:SQL查询专项训练(二)
面试必备杀技:SQL查询专项训练
|
3月前
|
SQL
面试必备杀技:SQL查询专项训练(一)
面试必备杀技:SQL查询专项训练
|
15天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10