[SQL brush questions] DAY20----SQL advanced connection special exercise

Blogger nickname: Stair Jump Penguin
Original intention: The original intention of this blog is to communicate with technical friends. Everyone's technology has shortcomings. The same is true for bloggers. I ask for advice with humility and hope that all technical friends can give guidance.
The blogger's motto: discover the light, follow the light, become the light, radiate the light;
the blogger's research direction: penetration testing, machine learning;
the blogger's message: thank you for your support, your support is the driving force for me to move forward;

Niu Ke brush questions: Niu Ke brush questions jump link

Table of contents

foreword

 1. Connection table

2. Brush questions

1. Topic 1

(1) Topics

(2) Difficulty of the question

(3) Example

(4) Code

(5) Running screenshot

2. Topic 2

(1) Topics

(2) Difficulty of the question

(3) Example

(4) Code

(5) Running screenshot

3. Topic 3

(1) Topics

(2) Difficulty of the question

(3) Example

(4) Code

(5) Running screenshot

4. Topic 4

(1) Topics

(2) Difficulty of the question

(3) Example

(4) Code

(5) Running screenshot

5. Topic 5

(1) Topics

(2) Difficulty of the question

(3) Example

(4) Code

(5) Running screenshot

 3. Summary


foreword

I recommend a better brushing station for everyone. The friends who will be recruited in the autumn will come and rush, learn, and roll up.

 1. Connection table

One of the most powerful features of SQL is the ability to join tables in the execution of a data retrieval query. Joins are made using SQL

The most important operation SELECT can perform, and a good understanding of joins and their syntax is an extremely important part of learning SQL.

 

2. Brush questions

1. Topic 1

(1) Topics

Title: Retrieve the name of each customer and all order numbers (1)
Description: Write a SQL statement using INNER JOIN to retrieve the name of each customer (cust_name in the Customers table) and all order numbers (order_num in the Orders table) , and finally return according to the customer name cust_name in ascending order.

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

输入:

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
	cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
	cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
);
INSERT `Customers` VALUES ('cust10','andy'),('cust1','ben'),('cust2','tony'),('cust22','tom'),('cust221','an'),('cust2217','hex');

DROP TABLE IF EXISTS `Orders`;
CREATE TABLE IF NOT EXISTS `Orders`(
  order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
  cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
);
INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a3','cust2'),('a4','cust22'),('a5','cust221'),('a7','cust2217');

输出:

an|a5
andy|a1
ben|a2
hex|a7
tom|a4
tony|a3

(4) Code

SELECT 
  C.cust_name, O.order_num
FROM
  Customers AS C
  INNER JOIN Orders AS O USING(cust_id)
ORDER BY
  C.cust_name

(5) Running screenshot

2. Topic 2

(1) Topics

Question: Retrieve the name and all order numbers of each customer (2)

Description: Retrieves the name of each customer (cust_name in the Customers table) and all order numbers (order_num in the Orders table), listing all customers even if they have not placed an order. Finally, return according to the customer name cust_name in ascending order.

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

输入:

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
	cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
	cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
);
INSERT `Customers` VALUES ('cust10','andy'),('cust1','ben'),('cust2','tony'),('cust22','tom'),('cust221','an'),('cust2217','hex'),('cust40','ace');

DROP TABLE IF EXISTS `Orders`;
CREATE TABLE IF NOT EXISTS `Orders`(
  order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
  cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
);
INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a3','cust2'),('a4','cust22'),('a5','cust221'),('a7','cust2217');

输出:

ace|None
an|a5
andy|a1
ben|a2
hex|a7
tom|a4
tony|a3

(4) Code

select
  cust_name,
  order_num
from
  Customers
  left join Orders using(cust_id)
order by
  cust_name asc

(5) Running screenshot

3. Topic 3

(1) Topics

Title: Return the product name and the order number associated with it

Description: Use an OUTER JOIN to join the Products table and the OrderItems table to return a list of product names (prod_name) and their associated order numbers (order_num), sorted in ascending order by product name.

(2) Difficulty of the question

Difficulty level of this question: easy

(3) Example

输入:

DROP TABLE IF EXISTS `Products`;
CREATE TABLE IF NOT EXISTS `Products` (
`prod_id` VARCHAR(255) NOT NULL COMMENT '产品 ID',
`prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称'
);
INSERT INTO `Products` VALUES ('a0001','egg'),
('a0002','sockets'),
('a0013','coffee'),
('a0003','cola'),
('a0023','soda');

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
	order_num VARCHAR(255) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a0001','a105'),('a0002','a1100'),('a0002','a200'),('a0013','a1121'),('a0003','a10'),('a0003','a19'),('a0003','a5');

输出:

coffee|a1121
cola|a5
cola|a19
cola|a10
egg|a105
sockets|a200
sockets|a1100
soda|None

(4) Code

select prod_name, order_num
from Products p left join OrderItems o on p.prod_id=o.prod_id
order by prod_name;

(5) Running screenshot

4. Topic 4

(1) Topics

Title: Return the product name and the total number of orders for each product
Description: Use OUTER JOIN to join the Products table and the OrderItems table, return the product name (prod_name) and the total number of orders for each product (not the order number), and by product Sort names in ascending order.

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

DROP TABLE IF EXISTS `Products`;
CREATE TABLE IF NOT EXISTS `Products` (
`prod_id` VARCHAR(255) NOT NULL COMMENT '产品 ID',
`prod_name` VARCHAR(255) NOT NULL COMMENT '产品名称'
);
INSERT INTO `Products` VALUES ('a0001','egg'),
('a0002','sockets'),
('a0013','coffee'),
('a0003','cola'),
('a0023','soda');

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	prod_id VARCHAR(255) NOT NULL COMMENT '产品id',
	order_num VARCHAR(255) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a0001','a105'),('a0002','a1100'),('a0002','a200'),('a0013','a1121'),('a0003','a10'),('a0003','a19'),('a0003','a5');

输出:

coffee|1
cola|3
egg|1
sockets|2
soda|0

(4) Code

SELECT prod_name,COUNT(order_num) 
FROM Products P
LEFT JOIN OrderItems O USING(prod_id)
GROUP BY prod_name
ORDER BY prod_name;

(5) Running screenshot

5. Topic 5

(1) Topics

Title: List Vendors and their Quantity of Available Products
Description: List Vendors (vend_id in the Vendors table) and their quantity of available products, including those that have no products. You need to use OUTER JOIN and the COUNT() aggregate function to count the quantity of each product in the Products table, and finally sort by vend_id in ascending order.

Note: The vend_id column appears in multiple tables, so it needs to be fully qualified every time it is referenced.

(2) Difficulty of the question

Difficulty level of this question: easy

(3) Example

输入:

DROP TABLE IF EXISTS `Vendors`;
CREATE TABLE IF NOT EXISTS `Vendors` (
  `vend_id` VARCHAR(255) NOT NULL COMMENT 'vend名称'
);
INSERT INTO `Vendors` VALUES ('a0002'),
('a0013'),
('a0003'),
('a0010');

DROP TABLE IF EXISTS `Products`;
CREATE TABLE IF NOT EXISTS `Products` (
`vend_id` VARCHAR(255) NOT NULL COMMENT '产品 ID',
`prod_id` VARCHAR(255) NOT NULL COMMENT '产品名称'
);
INSERT INTO `Products` VALUES ('a0001','egg'),
('a0002','prod_id_iphone'),
('a00113','prod_id_tea'),
('a0003','prod_id_vivo phone'),
('a0010','prod_id_huawei phone');

输出:

a0002|1
a0003|1
a0010|1
a0013|0

(4) Code

select a.vend_id, count(prod_id) as prod_id
from Vendors a 
left join Products b 
using(vend_id)
group by a.vend_id
order by a.vend_id 

(5) Running screenshot

 3. Summary

This article is the 20th day of SQL brushing questions

Welcome everyone and bloggers to brush the questions 0SQL brush questions click to start learning 2

Guess you like

Origin blog.csdn.net/weixin_50481708/article/details/126980067