[SQL brush questions] DAY19----SQL join table 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. Aggregate data

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 that 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: Return the customer name and related order number
Description: Write a SQL statement to return the customer name (cust_name) in the Customers table and the related order number (order_num) in the Orders table, and sort the results in ascending order by the customer name and then by the order number . You can try two different ways, one using simple equi-join syntax and one using INNER JOIN.

(2) Difficulty of the question

Difficulty level of this question: easy

(3) Example

输入:

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');

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');

输出:

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

(4) Code

SELECT cust_name, order_num
FROM Customers INNER JOIN Orders
ON Customers.cust_id = Orders.cust_id
ORDER BY cust_name, order_num;

(5) Running screenshot

2. Topic 2

(1) Topics

Title: Return the customer name and associated order number and the total price for each order

Description: In addition to returning the customer name and order number, return the customer name in the Customers table (cust_name) and the associated order number in the Orders table (order_num), add a third column OrderTotal, which contains the total price of each order, and order by customer Name then sorts the results in ascending order by order number.

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

输入:

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');

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 `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
  order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
  quantity INT(16) NOT NULL COMMENT '商品数量',
  item_price INT(16) NOT NULL COMMENT '商品价格'
);
INSERT `OrderItems` VALUES ('a1',1000,10),('a2',200,10),('a3',10,15),('a4',25,50),('a5',15,25),('a7',7,7);

输出:

an|a5|375
andy|a1|10000
ben|a2|2000
hex|a7|49
tom|a4|1250
tony|a3|150

(4) Code

SELECT cust_name, order_num, SUM(quantity * item_price) AS OrderTotal
FROM Customers 
INNER JOIN Orders USING(cust_id)
INNER JOIN OrderItems USING(order_num)
GROUP BY cust_name, order_num
ORDER BY cust_name, order_num;

(5) Running screenshot

3. Topic 3

(1) Topics

Topic: Determine which orders purchased the product with prod_id BR01 (2)

describe:

Write a SQL statement, use a subquery to determine which orders (in OrderItems) purchased the product with prod_id "BR01", and then return the customer ID (cust_id) and order date (order_date) corresponding to each product from the Orders table, press Sort the results in ascending order by order date.

Hint: This time use join and simple equal join syntax.

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

输入:

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 ('BR01','a0001'),('BR01','a0002'),('BR02','a0003'),('BR02','a0013');

  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',
    order_date TIMESTAMP NOT NULL COMMENT '下单时间'
  );
  INSERT `Orders` VALUES ('a0001','cust10','2022-01-01 00:00:00'),('a0002','cust1','2022-01-01 00:01:00'),('a0003','cust1','2022-01-02 00:00:00'),('a0013','cust2','2022-01-01 00:20:00');

输出:

cust10|2022-01-01 00:00:00
cust1|2022-01-01 00:01:00

(4) Code

select
  b.cust_id,
  b.order_date
from
  OrderItems a
  join Orders b using (order_num)
where
  a.prod_id = 'BR01'
order by
  b.order_date

(5) Running screenshot

4. Topic 4

(1) Topics

Title: Return the emails of all customers who purchased products whose prod_id is BR01 (2)
Description: There is a table OrderItems represents the order item information table, prod_id is the product id; Orders table represents the order table, cust_id represents the customer id and order_date; Customers table contains cust_email customer email and cust_id customer id

(2) Difficulty of the question

Difficulty level of this question: easy

(3) Example

输入:

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 ('BR01','a0001'),('BR01','a0002'),('BR02','a0003'),('BR02','a0013');

  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',
    order_date TIMESTAMP NOT NULL COMMENT '下单时间'
  );
  INSERT `Orders` VALUES ('a0001','cust10','2022-01-01 00:00:00'),('a0002','cust1','2022-01-01 00:01:00'),('a0003','cust1','2022-01-02 00:00:00'),('a0013','cust2','2022-01-01 00:20:00');

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
    cust_id VARCHAR(255) NOT NULL COMMENT '顾客id',
    cust_email VARCHAR(255) NOT NULL COMMENT '顾客email'
  );
INSERT `Customers` VALUES ('cust10','[email protected]'),('cust1','[email protected]'),('cust2','[email protected]');

输出:

[email protected]
[email protected]

(4) Code

select
  cust_email
from
  Customers
where
  cust_id in(
    select
      cust_id
    from
      Orders
      inner join OrderItems on OrderItems.order_num = Orders.order_num
    where
      prod_id = 'BR01'
  )

(5) Running screenshot

5. Topic 5

(1) Topics

Topic: Another way to determine the best customer (2)
Description: The OrderItems table represents order information. Another way to determine the best customer is to see how much they spent. The OrderItems table has the order number order_num and item_price. price, quantity product quantity

(2) Difficulty of the question

Difficulty level of this question: medium

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
	item_price INT(16) NOT NULL COMMENT '售出价格',
	quantity INT(16) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a1',10,105),('a2',1,1100),('a2',1,200),('a4',2,1121),('a5',5,10),('a2',1,19),('a7',7,5);


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');

输出:

andy|1050.000
ben|1319.000
tom|2242.000

(4) Code

select cust_name, sum(item_price * quantity) total_price
from Customers
inner join Orders using(cust_id)
inner join OrderItems using(order_num)
group by cust_name
having total_price >= 1000
order by total_price 

(5) Running screenshot

 

 3. Summary

This article is the 19th day of SQL brushing questions

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

Guess you like

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