[SQL brush questions] Day12----SQL summary 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. Aggregate data

Main function:

function describe
AVG() Returns the mean of the column
COUNT() Returns the row count of the column
MAX() Returns the maximum value of a column
MIN() Returns the minimum value of a column
SUM() Returns a summary of values ​​for a column

2. Brush questions

1. Brush Question 1

(1) Topics

Title: Determine the total number of products sold

Description : Write an SQL statement to determine the total number of products sold.

(2) Difficulty level

Difficulty Level: Beginner

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	quantity INT(16) NOT NULL COMMENT '商品数量'
);
INSERT `OrderItems` VALUES (10),(100),(1000),(10001),(2),(15);

输出:

items_ordered
11128.000

(4) Code

select sum(quantity) as items_ordered
from OrderItems

(5) Running screenshot

2. Brush question 2

(1) Topics

Title: Determine the total number of product item BR01 sold

Description: Modify the created statement to determine the total number of product items sold (prod_id) as "BR01".

(2) Difficulty level

Difficulty level: medium

(3) Example

输入:

DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	quantity INT(16) NOT NULL COMMENT '商品数量',
	prod_id VARCHAR(255) NOT NULL COMMENT '商品项'
);
INSERT `OrderItems` VALUES (10,'AR01'),(100,'AR10'),(1000,'BR01'),(10001,'BR010');

输出:

1000.000

(4) Code

select sum(quantity) from OrderItems
group by prod_id
having prod_id="BR01"

(5) Running screenshot

3. Brush question three

(1) Topics

Question: Determine the price of the most expensive product in the Products table that costs less than $10

Description: Write an SQL statement to determine the price (prod_price) of the most expensive product in the Products table whose price does not exceed $10. Name the calculated field max_price.

(2) Difficulty level

Difficulty level: medium

(3) Example

输入:

DROP TABLE IF EXISTS `Products`;
CREATE TABLE IF NOT EXISTS `Products` (
`prod_price` DOUBLE NOT NULL COMMENT '产品价格'
);
INSERT INTO `Products` VALUES (9.49),
(600),
(1000);

输出:

max_price
9.490

(4) Code

select prod_price  as  max_price
from Products
where prod_price <= 10
order by prod_price desc
limit 0 ,1 

(5) Running screenshot

 

3. Summary

This article is the twelfth day of a series of articles on MySQL brushing 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/126684349