Blogger nickname: Stair Jumping Penguin
Blogger Main Page Link: Home Portal Portal
Blogger Column Page Link: Network Security Column Portal
Creation Original Intention: The original intention of this blog is to communicate with technical friends, everyone's technology has shortcomings Board, bloggers are the same, ask for advice humbly, 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: website portal
Table of contents
(2) Difficulty of the question
(2) Difficulty of the question
(2) Difficulty of the question
(2) Difficulty of the question
(2) Difficulty of the question
1. SQL join table
One of the most powerful features of SQL is the execution of join tables (inner) in selects. A join table is the most important operation that can be performed using SQL's select statement, 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
Question: Retrieve the name and all order numbers of each customer (1)
Description: Use INNER JOIN to write a SQL statement 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 a left join OrderItems b using(prod_id)
union
select prod_name, order_num
from Products a right join OrderItems b using(prod_id)
order by prod_name
(5) Running screenshot
4. Topic 4
(1) Topics
Question: 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 sort by product name 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) as orders
from Products
left join OrderItems using(prod_id)
group by prod_name
order by 1
(5) Running screenshot
5. Topic 5
(1) Topics
Topic: List suppliers and their quantity of available products
Description: Lists vendors (vend_id in the Vendors table) and the number of products they have available, including vendors with 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 16th day of SQL brushing questions
Welcome everyone and bloggers to brush the questions SQL brush questions click to start learning