[SQL brush questions] DAY21----SQL combined query special exercise

Blogger nickname: Penguin Jumping Stairs
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 an humility and hope that all technical friends will 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. Combined query

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

 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. Combined query

UNION is very easy to use, but there are rules for its use.

1. UNION must be composed of two or more SELECTs. The statements are separated by UNION. Three SELECT statements use two UNIONs.

2. Each query of UNION must contain the same column, expression or aggregate function

3. Column data types must be compatible

2. Brush questions

1. Topic 1

(1) Topics

Title: Combining Two SELECT Statements (1)
Description: Combining two SELECT statements to retrieve the product id (prod_id) and quantity from the OrderItems table. Among them, one SELECT statement filters 100 rows, another SELECT statement filters products whose id starts with BNBG, and finally sorts the results in ascending order by product id.

(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',
	quantity VARCHAR(255) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a0001',105),('a0002',100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5),('BNBG',10002);

输出:

a0002|100.000
BNBG|10002.000

(4) Code

select prod_id,quantity 
from OrderItems
where quantity = 100
union
select prod_id,quantity 
from OrderItems
where prod_id like 'BNBG%'
order BY prod_id

(5) Running screenshot

 

2. Topic 2

(1) Topics

Topic: Combining two SELECT statements (2)

Description: Combines two SELECT statements to retrieve the product id (prod_id) and quantity from the OrderItems table. Among them, one SELECT statement filters 100 rows, another SELECT statement filters products whose id starts with BNBG, and finally sorts the results in ascending order by product 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',
	quantity VARCHAR(255) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a0001',105),('a0002',100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5),('BNBG',10002);

输出:

a0002|100.000
BNBG|10002.000

(4) Code

select prod_id, quantity
from OrderItems
where quantity = 100
or prod_id regexp '^BNBG'

(5) Running screenshot

3. Topic 3

(1) Topics

Question: Combine product names in Products table with customer names in Customers table

Description: Write a SQL statement that combines the product name (prod_name) in the Products table and the customer name (cust_name) in the Customers table and returns, then sorts the results 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_name` VARCHAR(255) NOT NULL COMMENT '产品名称'
);
INSERT INTO `Products` VALUES ('flower'),
('rice'),
('ring'),
('umbrella');

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
	cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
);
INSERT `Customers` VALUES ('andy'),('ben'),('tony'),('tom'),('an'),('lee'),('hex');

输出:

an
andy
ben
flower
hex
lee
rice
ring
tom
tony
umbrella

(4) Code

select prod_name
from Products
union all
select cust_name
from Customers
order by prod_name

(5) Running screenshot

 

4. Topic 4

(1) Topics

Title: Error Correction 4
Description: Fix the following erroneous SQL

(2) Difficulty of the question

Difficulty level of this question: Beginner

(3) Example

输入:

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
    cust_name VARCHAR(255) NOT NULL COMMENT '顾客id',
    cust_contact VARCHAR(255) NOT NULL COMMENT '顾客联系方式',
    cust_state VARCHAR(255) NOT NULL COMMENT '顾客州',
    cust_email VARCHAR(255) NOT NULL COMMENT '顾客email'
  );
INSERT `Customers` VALUES ('cust10','8695192','MI','[email protected]'),('cust1','8695193','MI','[email protected]'),('cust2','8695194','IL','[email protected]');

输出:

cust1|8695193.000|[email protected]
cust10|8695192.000|[email protected]
cust2|8695194.000|[email protected]

(4) Code

SELECT cust_name, cust_contact, cust_email 
FROM Customers 
WHERE cust_state = 'MI' 
UNION 
SELECT cust_name, cust_contact, cust_email 
FROM Customers 
WHERE cust_state = 'IL'
ORDER BY cust_name; 

(5) Running screenshot

 3. Summary

This article is the 21st 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/127032860