当前位置:网站首页>[introduction to SQL for 10 days] day4 Combined Query & specified selection
[introduction to SQL for 10 days] day4 Combined Query & specified selection
2022-06-28 08:25:00 【Ly methane】
1965. Employees who lost information
surface : Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
employee_id It's the primary key of this table . Each line represents the employee's id And his name .
surface : Salaries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| salary | int |
+-------------+---------+
employee_id is The primary key of this table . Each line represents the employee's id And his salary .
Write a query statement , Find all Employees full name lost , Or employee's Salary information Lost employees id.
Return the of these employees id employee_id , Sort from small to large .
Analysis of the answer
League table query , Full connection , however mysql It's not worth money , All can be left connected and connected together
Union: Combine two result sets , Do not include repeating lines , At the same time, sort the default rules ;
SELECT A.employee_id FROM employees A LEFT JOIN salaries B ON A.employee_id = B.employee_id
WHERE B.salary IS NULL
UNION
SELECT B.employee_id FROM employees A RIGHT JOIN salaries B ON A.employee_id = B.employee_id
WHERE A.name IS NULL
ORDER BY employee_id
1795. The price of each product in different stores
surface :Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
+-------------+---------+
The primary key of this table is product_id( product Id).
Each line stores this product in different stores store1, store2, store3 The price of . If this product is not sold in the store , The value will be null.
Please refactor Products surface , Check the price of each product in different stores ,
Make the output format change to (product_id, store, price) . If this product is not sold in the store , This line is not output .
In the output result table The order is not required .
Input :
Products table:
+------------+--------+--------+--------+
| product_id | store1 | store2 | store3 |
+------------+--------+--------+--------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
+------------+--------+--------+--------+
Output :
+------------+--------+-------+
| product_id | store | price |
+------------+--------+-------+
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
+------------+--------+-------+
Analysis of the answer
For row to column groupby, Column to row union all
union all and union The difference is that union all No weight removal
sumif
SELECT product_id, 'store1' store,store1 price
FROM products
WHERE store1 IS NOT NULL
UNION ALL
SELECT product_id, 'store2' store,store2 price
FROM products
WHERE store2 IS NOT NULL
UNION ALL
SELECT product_id, 'store3' store,store3 store
FROM products
WHERE store3 IS NOT NULL
608. Tree node
Given a table tree,id Is the number of the tree node , p_id Is its parent node id .
+----+------+
| id | p_id |
+----+------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+----+------+
Each node in the tree belongs to one of the following three types :
- leaf : If this node does not have any child nodes .
-- root : If this node is the root of the whole tree , That is, there is no parent node .
-- Internal node : If this node is neither a leaf node nor a root node .
Write a query statement , Output the number and type of all nodes , And sort the results according to the node number . The result of the above example is :
+----+------+
| id | Type |
+----+------+
| 1 | Root |
| 2 | Inner|
| 3 | Leaf |
| 4 | Leaf |
| 5 | Leaf |
+----+------+
Analysis of the answer
not in And null value , If not in There are null value , Then go straight back to null The corresponding result is false
ordinary case Several usages of the sentence
CASE WHEN condition THEN result ELSE result END
CASE WHEN p_id IS NULL THEN 'Root'
WHEN id NOT IN(SELECT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END
CASE SCORE WHEN 'A' THEN ' optimal ' ELSE ' fail, ' END
answer :
SELECT
id,
CASE WHEN p_id IS NULL THEN 'Root'
WHEN id NOT IN(SELECT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END AS 'type'
FROM tree
176. The second highest salary
Employee surface :
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id It's the primary key of this table .
Each row of the table contains the salary information of the employee .
Write a SQL Inquire about , Get and return Employee The second highest salary on the list . If there is no second highest salary , The query should return null .
Input :
Employee surface :
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output :
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
Analysis of the answer
limit usage :
select * from tableName limit i,n
# tableName: Table name
# i: Is the index value of the query result ( The default from the 0 Start ), When i=0 It can be omitted i
# n: Number returned for query results
# i And n Use English commas between "," separate
keyword distinct duplicate removal
SELECT DISTINCT name from A
Query the second largest , First query all , Then go heavy. , Then sort in descending order , And get limit Get the second
If the above operation does not find a value , Then return null, Then name the query content
SELECT IFNULL(
(SELECT DISTINCT salary
FROM employee
ORDER BY salary DESC
LIMIT 1, 1), null) SecondHighestSalary
summary :
- MySQL There is no external connection , Connect left and right + union all
- Break up the whole line into several lines union, Merge multiple rows into a whole row group by
- If not in There are null value , Then go straight back to null The corresponding result is false, It doesn't really judge whether there is in the collection
- case Sentence usage CASE WHEN condition THEN result1 ELSE result2 END
- DISTINCT duplicate removal SELECT DISTINCT name from A
- LIMIT usage take i - n, from 0 Start : SELECT * FROM table limit i,n
边栏推荐
- A - Bi-shoe and Phi-shoe
- 【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
- IO error in Oracle11g: got minus one from a read call
- 【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
- Selenium reptile
- Force buckle 1884 Egg drop - two eggs
- 【学习笔记】差分约束
- Idea related issues
- Kubernetes notes and the latest k3s installation introduction
- NLP sequence can completely simulate human brain intelligence
猜你喜欢

你了解TCP協議嗎(二)?

Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation

广州:金融新活水 文企新机遇

2022第六季完美童模 佛山赛区 初赛圆满落幕

探讨gis三维系统在矿山行业中的应用

The maximum number of Rac open file descriptors, and the processing of hard check failure

Why MySQL cannot insert Chinese data in CMD

Prometheus monitoring (I)

ROS notes (08) - definition and use of service data

Trigonometric transformation formula
随机推荐
广州:金融新活水 文企新机遇
DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...
抖音服务器带宽有多大,才能供上亿人同时刷?
App automated testing appium tutorial 2 - ADB command
【学习笔记】最短路 +生成树
Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
Eslint 语法监测关闭
Oracle view tablespace usage
Set the icon for the title section of the page
After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
【学习笔记】差分约束
AWS saves data on the cloud (3)
开户券商怎么选择?网上开户是否安全么?
Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
ROS 笔记(09)— 参数的查询和设置
Tree
Vagrant installation
TCP那点事
On the solution of insufficient swap partition