当前位置:网站首页>SQL programming task04 job - set operation

SQL programming task04 job - set operation

2022-06-23 01:21:00 Shallow look

1 Learning content

DataWhale SQL Team learning

2 The addition and subtraction of tables

What is set operation ?
Sets represent... In the field of mathematics “ The sum of all kinds of things ”, Represent in the database domain A collection of records . say concretely , surface 、 The execution results of views and queries are collections of records , The elements are each row in the table or query results . In standard SQL in , Use... For the search results respectively UNION, INTERSECT, EXCEPT To search the results and , Intersection and difference operations , image UNION,INTERSECT,EXCEPT This kind of operator used for set operation is called set operator .

2.1 Addition of tables UNION

2.1.1 UNION

SQL sentence :( Represents the union of two sets )

SELECT product_id, product_name
  FROM Product
 UNION
SELECT product_id, product_name
  FROM Product2;

notes :UNION Such set operators usually remove duplicate records

2.1.2 UNION And OR The predicate

SQL sentence :( The results are consistent )

--  Use  OR  The predicate 
SELECT * 
  FROM Product 
 WHERE sale_price / purchase_price < 1.3 
    OR sale_price / purchase_price IS NULL;

**--  Use  UNION
SELECT * 
  FROM Product 
 WHERE sale_price / purchase_price < 1.3
 UNION
SELECT * 
  FROM Product 
 WHERE sale_price / purchase_price IS NULL;
**

2.1.3 UNION ALL

Set operations that contain duplicate rows , stay UNION After add ALL that will do

2.2 MySQL 8.0 Intersection operations are not supported INTERSECT

2.3 Difference set , Complement and subtraction of tables

2.3.1 MySQL 8.0 Not yet EXCEPT operation

MySQL 8.0 Table subtraction operator is not yet supported EXCEPT. But with the help of NOT IN The predicate , We can also implement table subtraction .

Is only found in Product Table but does not exist in Product2 surface :

--  Use  IN  Clause 
SELECT * 
  FROM Product
 WHERE product_id NOT IN (SELECT product_id 
                            FROM Product2)

2.3.2 INTERSECT And AND The predicate

For two query results of the same table , Their friendship INTERSECT In fact, the retrieval conditions of the two queries can be used equivalently AND Predicate join to implement .

2.4 Symmetry difference

Two sets A,B The symmetry difference refers to those that belong only to A Or only belong to B A collection of elements . The intersection of two sets can be regarded as the intersection of two sets, and the symmetry difference between the two sets is removed .
But because in MySQL8.0 in , Because the of two tables or query results cannot be obtained directly . Therefore, it is not suitable to use the above idea to calculate the symmetry difference . Fortunately, there are difference set operations that can be used . You can see intuitively , The difference in symmetry between two sets is equal to A-B And go up B-A, Therefore, in practice, we can use this idea to find the symmetry difference .

Use Product Table and Product2 Table symmetry difference to query which products are only in one of the tables
SQL sentence :

--  Use  NOT IN  Implement the difference set of two tables 
SELECT * 
  FROM Product
 WHERE product_id NOT IN (SELECT product_id FROM Product2)
UNION
SELECT * 
  FROM Product2
 WHERE product_id NOT IN (SELECT product_id FROM Product)

3 Link (JOIN)

Time issues , The grammar rules will not be sorted out for the time being , Subsequent complement .
Details refer to :
DataWhale SQL Team learning
MySQL- - Set operations

4 Exercises

4.1

find product and product2 The middle price is higher than 500 The basic information of the products .
SQL sentence :

select * 
from product
where sale_price > 500
union
select * 
from product2
where sale_price > 500;

Running results :
 Insert picture description here

4.2

With the help of the implementation of symmetrical difference , seek product and product2 Intersection .
SQL sentence :

select * 
from (select * from product 
			union 
			select * from product2) as p
where product_id not in
			(SELECT product_id FROM product
			 WHERE product_id NOT IN (SELECT product_id FROM product2)
			 UNION
       		 SELECT product_id FROM product2
			 WHERE product_id NOT IN (SELECT product_id FROM product));

Running results :
 Insert picture description here

4.3

Which stores are selling the highest priced goods in each category ?
SQL sentence :

select p1.shop_id,p1.shop_name,p1.quantity,
			 p2.product_id,p2.product_name,p2.product_type,p2.sale_price,
			 mp.max_price as ' The highest selling price of this kind of goods is '
from shopproduct as p1
inner join product as p2
	on p1.product_id = p2.product_id
inner join (select product_type ,max(sale_price)as max_price
						from product
						group by product_type
						)as mp
on mp.product_type = p2.product_type
and p2.sale_price = mp.max_price;

Running results :
 Insert picture description here

4.4

Use the inner link and the associated sub link to query the goods with the highest selling price in each category .
Inner link SQL sentence :( Consistent with the above question )

select p.product_id,p.product_id,p.product_type,p.sale_price,
			 mp.max_price as ' The maximum price of this kind of commodity '
from product as p
inner join(select product_type,max(sale_price) as max_price
					 from product
					 group by product_type)as mp
on p.product_type = mp.product_type
and p.sale_price = mp.max_price;		 

Running results :
 Insert picture description here
Associated subquery SQL sentence :

select p.product_id,p.product_type,p.sale_price,
			 mp.max_price as ' The maximum price of this kind of commodity '
from product as p,
		 (select product_type,max(sale_price) as max_price
		  from product
			group by product_type) as mp
where p.product_type = mp.product_type
and p.sale_price = mp.max_price;

 Insert picture description here

4.5

Use associated sub query to realize : stay product In the table , Take out product_id, produc_name, slae_price, And sort according to the selling price of goods from low to high 、 Add up the selling price .
SQL sentence :

SELECT	p.product_id, p.product_name, p.sale_price,
				(select sum(sale_price) from product as p1
				 where p.sale_price > p1.sale_price
				 or(p.sale_price=p1.sale_price)
				 )as ' Cumulative sum '
from product as p
order by sale_price;   

Running results :
 Insert picture description here
Exercise reference :
Tianchi Dragon Ball SQL Training camp routine task4 Clock in

原网站

版权声明
本文为[Shallow look]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220918143063.html