当前位置:网站首页>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)
边栏推荐
猜你喜欢

Vscode -- create template file
![[training day3] section [greed] [two points]](/img/4f/4130a1ade0ac0003adeddca780ff14.png)
[training day3] section [greed] [two points]

windows10 安装Sql Server 2019
![[training day4] anticipating [expected DP]](/img/66/35153a9aa77e348cae042990b55b1c.png)
[training day4] anticipating [expected DP]

基于RoBERTa-wwm动态融合模型的中文电子病历命名实体识别
![[training day4] card game [greed]](/img/02/88af03ca5e137eba6cdd778f827f2b.png)
[training day4] card game [greed]

Some key information about Max animation (shift+v)

阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%

Schematic diagram of C measuring tool

Excellent basic methods of URL parsing using C language
随机推荐
Ncnn compilation and use pnnx compilation and use
【多线程的相关内容】
Hcip - OSPF comprehensive experiment
融合迁移学习与文本增强的中文成语隐喻知识识别与关联研究
利用C语言实现URL解析的基本方法之优秀
架构——MVC的升华
Small program completion work wechat campus laundry small program graduation design finished product (2) small program function
[training day3] delete [simulation]
Research on automatic classification of electronic medical records based on unbalanced data
watch VS watchEffect
poj3461 Oulipo【KMP】
第3章业务功能开发(添加线索备注,自动刷新添加内容)
HDU1422 重温世界杯【DP】
HDU4496 D-City【并查集】
Good architecture is evolved, not designed
Pure C handwriting thread pool
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
Excellent basic methods of URL parsing using C language
The difference between [x for X in list_a if not np.isnan (x)] and [x if not np.isnan (x) else none for X in list_a]
2022牛客多校二_ E I