[SQL brush questions] DAY14----Special exercise on using subqueries in SQL

​Blogger Nickname: Jumping Stairs Penguin
Blogger Main Page Link: Blogger Home Portal

Blogger's column page link: Column Portal -- Network Security Technology
Creation Original Intention: The original intention of this blog is to communicate with technical friends. Everyone's technology has shortcomings, and the same is true for bloggers. Ask for advice with an humility. Friends give guidance.
The blogger's motto: discover the light, follow the light, become the light, and emit 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;

Learning website jump link: Niuke Brush Questions Network

 

foreword

Recommend a very good question brushing software for everyone. Niuke Brush Questions Network---Learn SQL together

 Why do bloggers like to use this website to learn?

There are three main reasons:

1. Contains a large number of interview question banks

2. The coverage of the industry is relatively comprehensive

3. The questions of brushing questions are in accordance with the process from simple to difficult

1. SQL uses subqueries

(1) Subquery

A subquery is a query nested in another statement, such as: select , insert, update, delete

(2) Nested subqueries

Subqueries can be nested within another subquery, SQL Server supports up to 32 nesting levels

(3) Related subroutines

① A correlated subquery is a subquery that uses the values ​​of the outer query . i.e. it depends on the value of the outer query

② Correlated subqueries cannot be executed independently as simple subqueries

③ Repeatedly execute the correlated subquery once for each row evaluated by the outer query. Correlated subqueries are also known as repeating subqueries .

2. Brush questions

1. Brush Question 1

(1) Topics

Question: Return a list of customers who purchased products for $10 or more

Description: OrderItems represents the order item table, containing the fields order number: order_num, order price: item_price; the Orders table represents the order information table, including customer id: cust_id and order number: order_num

(2) 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 '售出价格'

  );

  INSERT `OrderItems` VALUES ('a1',10),('a2',1),('a2',1),('a4',2),('a5',5),('a2',1),('a7',7);


  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'),('a2','cust1'),('a4','cust2'),('a5','cust5'),('a2','cust1'),('a7','cust7');


输出:


cust10

(3) Code

select cust_idfrom Orderswhere order_num in (

    select order_num

    from OrderItems

    where item_price >=10

)

(4) Operation result

2. Brush question 2

(1) Topics

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

Description: The table OrderItems represents the order item information table, prod_id is the product id; the Orders table represents the order table, and the cust_id represents the customer id and the order date order_date

(2) 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

(3) Code

select cust_id,order_datefrom Orderswhere order_num in

(select order_numfrom OrderItemswhere prod_id = 'BR01')

(4) Operation result

3. Brush question three

(1) Topics

Title: Return the emails of all customers who purchased products with prod_id BR01 (1)

Description: You want to know the date of ordering BR01 products, there is a table OrderItems representing order item information table, prod_id is product id; Orders table represents order table, cust_id represents customer id and order date order_date; Customers table contains cust_email customer email and cust_id customer id

 

(2) 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]

(3) Code

select

    Customers.cust_emailfrom

    Ordersleft join OrderItems on OrderItems.order_num = Orders.order_numleft join Customers on Customers.cust_id = Orders.cust_idwhere

    OrderItems.prod_id = "BR01"

(4) Operation result

4. Brush Question 4

(1) Topics

Question: Return the total amount of each customer's different orders

Description: We need a list of customer IDs containing the total amount they have ordered. The OrderItems table represents order information, and the OrderItems table has the order number: order_num, the selling price of the item: item_price, and the quantity of the item: quantity.

 

(2) 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 ('a0001',10,105),('a0002',1,1100),('a0002',1,200),('a0013',2,1121),('a0003',5,10),('a0003',1,19),('a0003',7,5);

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 ('a0001','cust10'),('a0003','cust1'),('a0013','cust2');


输出:


cust2|2242.000

cust10|1050.000

cust1|104.000

(3) Code

SELECT 

    o.cust_id,

    sum(oi.item_price * oi.quantity) AS total_orderedFROM Orders o JOIN OrderItems oi

    USING (order_num)GROUP BY o.cust_idORDER BY total_ordered DESC

(4) Operation result

 

5. Brush Question 5

(1) Topics

Question: Retrieve all product names and corresponding sales totals from the Products table

Description: Retrieve all product names in the Products table: prod_name, product id: prod_id

OrderItems represents the order item table, order product: prod_id, sold quantity: quantity

 

(2) Example

enter:

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

DROP TABLE IF EXISTS `OrderItems`;CREATE TABLE IF NOT EXISTS `OrderItems`(

prod_id VARCHAR(255) NOT NULL COMMENT '产品id',

quantity INT(16) NOT NULL COMMENT '商品数量'

);INSERT `OrderItems` VALUES ('a0001',105),('a0002',1100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5);


输出:


egg|105.000

sockets|1300.000

coffee|1121.000

cola|34.000

(3) Code

select

  prod_name,

  sum(quantity) as quant_soldfrom

  Products,

  OrderItemswhere

  OrderItems.prod_id = Products.prod_idgroup by

  prod_name

(4) Operation result

This article is the fourteenth day of SQL brushing questions

Welcome friends, click the link on the right to learn with the blogger, click to learn

Guess you like

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