当前位置:网站首页>[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
边栏推荐
- About RAC modifying scan IP
- Prometheus + grafana + MySQL master-slave replication + host monitoring
- App automated testing appium tutorial 2 - ADB command
- js取整的小技巧
- Devops Basics: Jenkins deployment and use (I)
- Understanding of CUDA, cudnn and tensorrt
- [learning notes] simulation
- npm清理缓存
- 【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
- 【学习笔记】模拟
猜你喜欢
B_QuRT_User_Guide(27)
Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
Eslint syntax monitoring off
爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!
Do you know TCP protocol (1)?
找合适的PMP机构只需2步搞定,一查二问
Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
设置网页的标题部分的图标
About using font icons in placeholder
Two tips for block level elements
随机推荐
PMP从报考到拿证基本操作,了解PMP必看篇
Robot Rapping Results Report
匿名页的反向映射
DB
FatMouse and Cheese
【力扣10天SQL入门】Day5+6 合并表
cuda和cudnn和tensorrt的理解
Oracle view all tablespaces in the current library
A - Bi-shoe and Phi-shoe
The RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
【学习笔记】最短路 +生成树
小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
解决npm ERR! Unexpected end of JSON input while parsing near问题
ROS 笔记(09)— 参数的查询和设置
npm清理缓存
Hidden scroll bar on PC
Unity - use of API related to Pico development input system ---c
【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
神殿
Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19