当前位置:网站首页>力扣SQL语句习题,错题记录
力扣SQL语句习题,错题记录
2022-07-27 13:09:00 【不爱健身的数学子弟不是好程序员】

本博客是记录力扣SQL习题中错题。(原题传送门)
目录
1.寻找用户推荐人
给定表 customer ,里面保存了所有客户信息和他们的推荐人。
+------+------+-----------+
| id | name | referee_id|
+------+------+-----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+------+------+-----------+写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。
对于上面的示例数据,结果为:
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+SQL语句:
IFNULL(expression_1,expression_2);
如果expression_1为NULL,则IFNULL函数返回expression_2; 否则返回expression_1的结果。
select name from customer where ifnull(referee_id,0)!=22. 计算特殊奖金
写出一个SQL 查询语句,计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以'M'开头,那么他的奖金是他工资的100%,否则奖金为0。返回的结果集请按照employee_id排序。
查询结果格式如下面的例子所示。
输入:
Employees 表:
+-------------+---------+--------+
| employee_id | name | salary |
+-------------+---------+--------+
| 2 | Meir | 3000 |
| 3 | Michael | 3800 |
| 7 | Addilyn | 7400 |
| 8 | Juan | 6100 |
| 9 | Kannon | 7700 |
+-------------+---------+--------+
输出:
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2 | 0 |
| 3 | 0 |
| 7 | 7400 |
| 8 | 0 |
| 9 | 7700 |
+-------------+-------+
解释:
因为雇员id是偶数,所以雇员id 是2和8的两个雇员得到的奖金是0。
雇员id为3的因为他的名字以'M'开头,所以,奖金是0。
其他的雇员得到了百分之百的奖金。
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。SQL语句:
Mod(a,b) :a / b 的余数;
LEFT(字段名,下标):返回'字段名'左起'下标'个字符。
select employee_id,if(mod(employee_id,2)!=0
and left(name,1)!='M',salary,0) bonus
from employees order by employee_id3.删除重复邮件
编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。以 任意顺序 返回结果表。查询结果格式如下所示。
输入:
Person 表:
+----+------------------+
| id | email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
输出:
+----+------------------+
| id | email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
+----+------------------+
解释: [email protected]重复两次。我们保留最小的Id = 1。SQL语句:
DELETE from Person Where Id not in (
Select Id From(Select MIN(Id) as id From Person Group by Email) t)
边栏推荐
- What are the benefits of taking NPDP
- 【idea】设置提取serialVersionUID
- Wechat campus laundry applet graduation design finished product of applet completion work (8) graduation design thesis template
- Alibaba's latest equity exposure: Softbank holds 23.9% and caichongxin holds 1.4%
- Group division and characteristic analysis of depression patients based on online consultation records
- Download address of each version of libtorch
- HDU4496 D-City【并查集】
- [training day3] delete [simulation]
- 基于RoBERTa-wwm动态融合模型的中文电子病历命名实体识别
- Positive mask, negative mask, wildcard
猜你喜欢

面向不平衡数据的电子病历自动分类研究

知识关联视角下金融证券知识图谱构建与相关股票发现

NoSQL -- three theoretical cornerstones of NoSQL -- cap -- Base -- final consistency

平板模切机

Recursive method to realize the greatest common divisor

Motion attitude control system of DOF pan tilt based on stm32

Chapter 3 business function development (view clue details)
![[training day4] card game [greed]](/img/02/88af03ca5e137eba6cdd778f827f2b.png)
[training day4] card game [greed]

Wechat campus laundry applet graduation design finished product (4) opening report

Cognition -- classic of the road to success of hardware engineers
随机推荐
面试八股文之·TCP协议
连接ResourceManager 失败
【图论】负环
West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion
Group division and characteristic analysis of depression patients based on online consultation records
In the "meta cosmic space", utonmos will open the digital world of the combination of virtual and real
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
printf函数缓冲区问题
There is no need for semantic segmentation of annotation data! Eth & Leuven University proposed maskdistill, using transformer for unsupervised semantic segmentation, SOTA
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
Travel notes from July 11 to August 1, 2022
Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers
UTNet 用于医学图像分割的混合Transformer
Real image denoising based on multi-scale residual dense blocks and block connected cascaded u-net
基于C语言的LR1编译器设计
Chapter 3 business function development (view clue details)
基于企业知识图谱的企业关联关系挖掘
HDU4496 D-City【并查集】
A Keypoint-based Global Association Network for Lane Detection