[SQL brush questions] Day13----SQL group data special exercise

Blogger Nickname: Jumping Stairs Penguin
Blogger Home 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. Grouping

Summary: Grouping is established in the GROUP BY clause of the SELECT statement. Grouping allows data to be divided into logical groups so that aggregate calculations can be performed on each group

Main function:

ORDER BY GROUP BY
Sorting produces output Group rows. but the output may not be in the order of the groups
Any column can be used (even non-selected columns can be used) Only select columns or expression columns are possible, and each select column expression must be used
not necessarily required If you use a column (or expression) with an aggregate function, you must use

2. Brush questions

1. Brush Question 1

(1) Topics

Question: Return the number of rows for each order number

Description : Write an SQL statement to return the number of rows (order_lines) of each order number (order_num), and sort the results in ascending order by order_lines.

(2) Difficulty level

Difficulty Level: Beginner

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
);
INSERT `OrderItems` VALUES ('a002'),('a002'),('a002'),('a004'),('a007');

输出:

a004|1
a007|1
a002|3

(4) Code

select order_num,count(*) as order_lines
from OrderItems
group by order_num
order by order_lines

(5) Running screenshot

 

2. Brush question 2

(1) Topics

Topic: Lowest cost product per supplier

Description: Write an SQL statement that returns a field named cheapest_item that contains the lowest cost product per supplier (using prod_price from the Products table), and then sort the results in ascending order from lowest cost to highest cost.

(2) Difficulty level

Difficulty level: medium

(3) Example

输入:

DROP TABLE IF EXISTS `Products`;
CREATE TABLE IF NOT EXISTS `Products` (
`vend_id` VARCHAR(255) NOT NULL COMMENT '供应商ID',
`prod_price` DOUBLE NOT NULL COMMENT '产品价格'
);
INSERT INTO `Products` VALUES ('a0011',100),
('a0019',0.1),
('b0019',1000),
('b0019',6980),
('b0019',20);

输出:

vend_id|cheapest_item
a0019|0.100
b0019|20.000
a0011|100.000

(4) Code

select vend_id,prod_price as cheapest_item
from (
    select row_number() over(partition by vend_id order by prod_price asc) as row_num,
            vend_id,prod_price
    from Products) temp
where row_num = 1
order by cheapest_item;     

(5) Running screenshot

3. Brush question three

(1) Topics

Question: Return the order number of all orders whose total order quantity is not less than 100

Description: Please write a SQL statement to return all order numbers whose total order quantity is not less than 100. The final result is sorted in ascending order by order number.

(2) Difficulty level

Difficulty level: medium

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
	quantity INT(255) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES ('a1',105),('a2',200),('a4',1121),('a5',10),('a7',5);

输出:

a1
a2
a4

(4) Code

SELECT order_num
FROM OrderItems
GROUP BY order_num
HAVING SUM(quantity) >= 100
ORDER BY order_num

(5) Running screenshot

4. Brush Question 4

(1) Topics

Topic: Calculate the sum

Description: Write a SQL statement, aggregate according to the order number, return all order numbers whose total order price is not less than 1000, and sort the final result in ascending order by order number. Hint: total price = item_price multiplied by quantity

(2) Difficulty level

Difficulty level: 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);

输出:

a1|1050.000
a2|1319.000
a4|2242.000

(4) Code

select order_num,sum(item_price*quantity) total_price from OrderItems
group by order_num
having total_price>=1000
order by order_num

(5) Running screenshot

5. Brush Question 5

(1) Topics

Subject: Error Correction 3

Description : Execute after modifying the following code correctly

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY items 
HAVING COUNT(*) >= 3 
ORDER BY items, order_num;

(2) Difficulty level

Difficulty level: easy

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	order_num VARCHAR(255) NOT NULL COMMENT '商品订单号'
);
INSERT `OrderItems` VALUES ('a002'),('a002'),('a002'),('a004'),('a007');

输出:

a002|3

(4) Code

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING items >= 3 
ORDER BY items, order_num;

(5) Running screenshot

3. Summary

This article is the thirteenth day of a series of MySQL essay questions!

Welcome friends to brush the questions together, click on the link on the right ------ brush questions network

Guess you like

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