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

Two tips for block level elements

Prometheus service discovery

Selenium+chromedriver cannot open Google browser page

VMware Workstation related issues

Why MySQL cannot insert Chinese data in CMD

【学习笔记】拟阵

Eslint 语法监测关闭

图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换

ROS 笔记(09)— 参数的查询和设置

AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
随机推荐
[learning notes] linear basis
Force buckle 1884 Egg drop - two eggs
Sword finger offer 30 Stack containing min function
【学习笔记】差分约束
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
About using font icons in placeholder
TCP那点事
Not so Mobile
[learning notes] matroid
ROS 笔记(09)— 参数的查询和设置
Infinite penetration test
After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
Preparation for Oracle 11g RAC deployment on centos7
三体攻击(三维拆分加二分)
Tree
B_QuRT_User_Guide(26)
找合适的PMP机构只需2步搞定,一查二问