当前位置:网站首页>Hard deduction SQL statement exercises, wrong question records
Hard deduction SQL statement exercises, wrong question records
2022-07-27 14:14:00 【Math kids who don't like fitness are not good programmers】

This blog is a record of Li Kou SQL Wrong questions in exercises .( Original title transmission gate )
Catalog
1. Looking for user references
1. Looking for user references
Given table customer , It contains all the customer information and their references .
+------+------+-----------+
| id | name | referee_id|
+------+------+-----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+------+------+-----------+Write a query statement , Return a list of customers , The number of the customer's recommender in the list is No 2.
For the sample data above , The result is :
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+SQL sentence :
IFNULL(expression_1,expression_2);
If expression_1 by NULL, be IFNULL The function returns expression_2; Otherwise return to expression_1 Result .
select name from customer where ifnull(referee_id,0)!=22. Calculate special bonuses
Write a SQL Query statement , Calculate the bonus for each employee . If an employee's id It's odd and his name doesn't start with 'M' start , Then his bonus is his salary 100%, Otherwise, the bonus is 0. For the returned result set, please follow employee_id Sort .
The query result format is shown in the following example .
Input :
Employees surface :
+-------------+---------+--------+
| employee_id | name | salary |
+-------------+---------+--------+
| 2 | Meir | 3000 |
| 3 | Michael | 3800 |
| 7 | Addilyn | 7400 |
| 8 | Juan | 6100 |
| 9 | Kannon | 7700 |
+-------------+---------+--------+
Output :
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2 | 0 |
| 3 | 0 |
| 7 | 7400 |
| 8 | 0 |
| 9 | 7700 |
+-------------+-------+
explain :
Because employees id It's even , So employees id yes 2 and 8 The bonus for two of our employees is 0.
Employee id by 3 Because his name is 'M' start , therefore , Bonus is 0.
The other employees got a 100% bonus .
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .SQL sentence :
Mod(a,b) :a / b The remainder of ;
LEFT( Field name , Subscript ): return ' Field name ' From the left ' Subscript ' Characters .
select employee_id,if(mod(employee_id,2)!=0
and left(name,1)!='M',salary,0) bonus
from employees order by employee_id3. Delete duplicate messages
Write a SQL Delete statements to Delete All duplicate emails , Keep only one id The smallest and only email . With In any order Return result table . The query result format is as follows .
Input :
Person surface :
+----+------------------+
| id | email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
Output :
+----+------------------+
| id | email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
+----+------------------+
explain : [email protected] Repeat twice . We keep the smallest Id = 1.SQL sentence :
DELETE from Person Where Id not in (
Select Id From(Select MIN(Id) as id From Person Group by Email) t)
边栏推荐
- LeetCode·每日一题·592.分数加减运算·模拟
- np.arange()和 range()的用法及区别
- [training day3] section [greed] [two points]
- Good architecture is evolved, not designed
- 基于C语言的LR1编译器设计
- Deep confidence network (DBN) [the classical DBN network structure is a deep neural network composed of several layers of RBM (restricted Boltzmann machine) and one layer of BP]
- 基于C语言实现线性表的建立、插入、删除、查找等基本操作
- This points to problems, closures, and recursion
- Wechat campus laundry applet graduation design finished product (7) Interim inspection report
- The salary level of programmers in various countries is a little miserable
猜你喜欢

Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China

RTL8762DK 环境搭建(一)

Schematic diagram of C measuring tool

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

平板模切机

Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers

Shell编程规范与变量

WPF visifire.charts4.6.1 tutorial with source code

基于招聘广告的岗位人才需求分析框架构建与实证研究

RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
随机推荐
Good architecture is evolved, not designed
面向流行性疾病科普的用户问题理解与答案内容组织
文旅数藏 | 用艺术的方式游云南
基于招聘广告的岗位人才需求分析框架构建与实证研究
NFT 的 10 种实际用途
Deep confidence network (DBN) [the classical DBN network structure is a deep neural network composed of several layers of RBM (restricted Boltzmann machine) and one layer of BP]
Positive mask, negative mask, wildcard
Travel notes from July 11 to August 1, 2022
平板模切机
PCL common operations
阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%
[related contents of multithreading]
初学者入门:使用WordPress搭建一个专属自己的博客
万字详解 Google Play 上架应用标准包格式 AAB
printf函数缓冲区问题
基于预训练模型的多标签专利分类研究
SLAM综述阅读笔记四:A Survey on Deep Learning for Localization and Mapping: Towards the Age of Spatial 2020
【2022-07-25】
SLAM综述阅读笔记七:Visual and Visual-Inertial SLAM: State of the Art, Classification,and Experimental 2021
windows10 安装Sql Server 2019