当前位置:网站首页>[introduction to SQL in 10 days] day5+6 consolidated table
[introduction to SQL in 10 days] day5+6 consolidated table
2022-06-28 08:25:00 【Ly methane】
175. Combine two tables
surface : Person
+-------------+---------+
| Name | type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId Is the primary key column of the table .
This table contains some people's ID And their last and first names .
+-------------+---------+
| Name | type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId Is the primary key column of the table . Each row of the table contains a ID = PersonId Information about people's cities and states .
Write a SQL Query to report Person The last name of each person in the list 、 name 、 Cities and states . If personId Your address is not in Address In the table , The report is empty null .
Input :
Person surface :
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address surface :
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output :
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
Analysis of the answer
Left connection , personId Same connection in one piece
SELECT A.firstName, A.lastName, B.city, B.state
FROM person A LEFT JOIN address B ON A.personId = B.personId
1581. Customers who enter the store without trading
surface :Visits
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| visit_id | int |
| customer_id | int |
+-------------+---------+
visit_id Is the primary key of the table . This table contains information about customers who have visited the shopping center .
surface :Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+----------------+---------+
transaction_id Is the primary key of this table . This table contains visit_id Information on transactions conducted during .
Some customers may visit the shopping center but do not trade . Please write a SQL Inquire about , To find these customers ID , And the number of times they only patronize and don't trade .
Return to In any order Sorted result table .
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54 | 2 |
| 30 | 1 |
| 96 | 1 |
+-------------+----------------+
Analysis of the answer
First, find out through the joint statement that there has been no transaction visit,
Then, these query results are processed according to customer_id grouping , Statistics customer_id And the number of each group
SELECT customer_id, count(visit_id) count_no_trans
FROM (
SELECT distinct A.visit_id, A.customer_id
FROM visits A LEFT JOIN transactions B ON A.visit_id = B.visit_id
WHERE B.amount IS NULL) C
GROUP BY C.customer_id
You can also check directly visits Not in transactions The data in , Then calculate in groups customer_id The same number
SELECT customer_id, count(visit_id) count_no_trans
FROM visits
WHERE visit_id NOT IN (SELECT distinct visit_id FROM transactions)
GROUP BY customer_id```
1148. Article browsing I
Views surface :
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| article_id | int |
| author_id | int |
| viewer_id | int |
| view_date | date |
+---------------+---------+
This table has no primary key , Therefore, there may be duplicate lines .
Each row of this table indicates that someone browsed an article by an author on a certain day .
Please note that , Of the same person author_id and viewer_id It's the same .
Please write one SQL Query to find all authors who have browsed their own articles , Results according to id Ascending order .
Views surface :
+------------+-----------+-----------+------------+
| article_id | author_id | viewer_id | view_date |
+------------+-----------+-----------+------------+
| 1 | 3 | 5 | 2019-08-01 |
| 1 | 3 | 6 | 2019-08-02 |
| 2 | 7 | 7 | 2019-08-01 |
| 2 | 7 | 6 | 2019-08-02 |
| 4 | 7 | 1 | 2019-07-22 |
| 3 | 4 | 4 | 2019-07-21 |
| 3 | 4 | 4 | 2019-07-21 |
+------------+-----------+-----------+------------+
Result sheet :
+------+
| id |
+------+
| 4 |
| 7 |
+------+
Analysis of the answer
Inquire about author_id and viewer_id equal
SELECT distinct author_id id
FROM views
WHERE author_id = viewer_id
ORDER BY id
197. The rising temperature
surface : Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id It's the primary key of this table , The table contains temperature information for a specific date
Write a SQL Inquire about , To find out ( Yesterday's ) Of all dates with a higher temperature id .
Return results Order is not required .
Input :
Weather surface :
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+
Output :
+----+
| id |
+----+
| 2 |
| 4 |
+----+
explain :
2015-01-02 The temperature is higher than the day before (10 -> 25)
2015-01-04 The temperature is higher than the day before (20 -> 30)
Analysis of the answer
DATEDIFF stay MySQL In the middle of the day
DATEDIFF(date1,date2) # return date1 - date2 Days of difference
Self join , The day after the query and the temperature is high
SELECT A.id
FROM weather A, Weather B
WHERE A.Temperature > B.Temperature AND DATEDIFF(A.recordDate, B.recordDate) = 1
607. Salesperson
surface : SalesPerson
+-----------------+---------+
| Column Name | Type |
+-----------------+---------+
| sales_id | int |
| name | varchar |
| salary | int |
| commission_rate | int |
| hire_date | date |
+-----------------+---------+
sales_id Is the primary key column of the table . Each row of the table shows the salesperson's name and ID , And their wages 、 Commission rate and date of employment .
surface : Company
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| com_id | int |
| name | varchar |
| city | varchar |
+-------------+---------+
com_id Is the primary key column of the table . Each row of the table indicates the name of the company and ID , And the city where the company is located .
surface : Orders
+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id | int |
| order_date | date |
| com_id | int |
| sales_id | int |
| amount | int |
+-------------+------+
order_id Is the primary key column of the table .
com_id yes Company In the table com_id The foreign key .
sales_id It's from the salesperson table sales_id The foreign key .
Each row of the table contains the information of an order . This includes the company's ID 、 Salesperson's ID 、 Date of order and amount paid .
Write a SQL Inquire about , The report has nothing to do with the name “RED” The names of all salespeople for orders related to the company .
Input :
SalesPerson surface :
+----------+------+--------+-----------------+------------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+------------+
| 1 | John | 100000 | 6 | 4/1/2006 |
| 2 | Amy | 12000 | 5 | 5/1/2010 |
| 3 | Mark | 65000 | 12 | 12/25/2008 |
| 4 | Pam | 25000 | 25 | 1/1/2005 |
| 5 | Alex | 5000 | 10 | 2/3/2007 |
+----------+------+--------+-----------------+------------+
Company surface :
+--------+--------+----------+
| com_id | name | city |
+--------+--------+----------+
| 1 | RED | Boston |
| 2 | ORANGE | New York |
| 3 | YELLOW | Boston |
| 4 | GREEN | Austin |
+--------+--------+----------+
Orders surface :
+----------+------------+--------+----------+--------+
| order_id | order_date | com_id | sales_id | amount |
+----------+------------+--------+----------+--------+
| 1 | 1/1/2014 | 3 | 4 | 10000 |
| 2 | 2/1/2014 | 4 | 5 | 5000 |
| 3 | 3/1/2014 | 1 | 1 | 50000 |
| 4 | 4/1/2014 | 1 | 4 | 25000 |
+----------+------------+--------+----------+--------+
Output :
+------+
| name |
+------+
| Amy |
| Mark |
| Alex |
+------+
According to the table orders Order in '3' and '4' , It's easy to see that there is only 'John' and 'Pam' Two salesmen once told the company 'RED' Sold .
So we need output meters salesperson The names of all the others in .
Analysis of the answer
First in Company Find the company in the table RED The company ID
And then Orders Find out in the table that the company's order involves sales_id
Last query SalesPerson In the table Query the names of people who are not in the involved list
SELECT A.name
FROM salesperson A
WHERE A.sales_id NOT IN(SELECT C.sales_id as id
FROM orders C
WHERE C.com_id IN(SELECT B.com_id
FROM Company B
WHERE B.name = 'RED'))
summary
- League table query , Left connection
- GROUP BY and count
- DATEDIFF(date1,date2) # return date1 - date2 Days of difference
- Classic subquery
边栏推荐
- Robot Rapping Results Report
- How to choose an account opening broker? Is it safe to open an account online?
- Unity 获取当前物体正前方,一定角度、距离的坐标点
- CloudCompare&PCL 点云SVD分解
- B_QuRT_User_Guide(27)
- TCP那点事
- Prometheus deployment alarm docking QQ mailbox
- Sword finger offer 30 Stack containing min function
- Prometheus service discovery
- [learning notes] linear basis
猜你喜欢

In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde

Vagrant installation

Do you know TCP protocol (1)?

B_ QuRT_ User_ Guide(26)

图像翻译:UVCGAN: UNET VISION TRANSFORMER CYCLE-CONSISTENT GAN FOR UNPAIRED IMAGE-TO-IMAGE TRANSLATION

AWS builds a virtual infrastructure including servers and networks (2)

AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》

MySQL implements transaction persistence using redo logs

VMware Workstation related issues

小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
随机推荐
The Falling Leaves
js取整的小技巧
RAC enable archive log
Tree
Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)
A - 深海探险
Prometheus service discovery
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
npm清理缓存
【学习笔记】最短路 +生成树
图像翻译:UVCGAN: UNET VISION TRANSFORMER CYCLE-CONSISTENT GAN FOR UNPAIRED IMAGE-TO-IMAGE TRANSLATION
The RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
关于如何在placeholder中使用字体图标
Kali installation configuration
【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
yaml json
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
开户券商怎么选择?网上开户是否安全么?
Understanding of CUDA, cudnn and tensorrt
How to choose an account opening broker? Is it safe to open an account online?