当前位置:网站首页>Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VI)
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VI)
2022-07-21 04:09:00 【18-year-old hates Java】
This content is divided into ten chapters , Five questions each time , Other chapters are on the homepage Mysql Column
Preface
Are you still worrying SQL Where to start , Or learned SQL I want to find a place to practice my hand ? What a coincidence , Recently, after work, I boarded Cattle guest , Found out Cattle guest I don't know when it went online SQL Will know The exercises of .
《SQL Will know 》 As MIT 、 Erie Reference textbooks of many universities such as neu University , From simple to deep SQL The basic concepts and grammar of . Involving data Sort 、 Filtering and grouping , And the watch 、 View 、 coupling 、 Subquery 、 The cursor 、 Stored procedures and triggers . Examples abound , Convenient access , It can be said that as a CRUD BOY/GIRL A must read list .
Just think about brushing it again , And then put yourself Brush problem To sum up , So here's today's article , I hope I can give a little help to my little friend in need
SQL26 determine Products The price in the table does not exceed 10 The price of the most expensive product in dollars
describe
Products surface
prod_price |
---|
9.49 |
600 |
1000 |
problem
To write SQL sentence , determine Products The price in the table does not exceed 10 The price of the most expensive product in dollars (prod_price). Name the calculated field max_price.
Sample results
return max_price
max_price |
---|
9.49 |
Example analysis
Return the highest price below ten yuan max_price.
Example
1 2 3 4 5 6 7 |
|
answer
Investigate knowledge points :
- Conditions of the query : Use keywords
WHERE
, The price of the product shall not exceed 10 Dollar products are screened out . - MAX( Name ): Find the maximum value in the column .
- Take the alias : By keyword
AS
Will not exceed 10 The record with the highest price among the products in USD will be renamed after screening .
1 |
|
SQL27 Returns the number of lines for each order number
describe
OrderItems The table contains each product for each order
order_num |
---|
a002 |
a002 |
a002 |
a004 |
a007 |
problem
To write SQL sentence , Return each order number (order_num) How many lines are there (order_lines), And press order_lines Ascending the results Sort .
Sample results
Return order No order_num And the number of lines corresponding to the order number order_lines
order_num | order_lines |
---|---|
a004 | 1 |
a007 | 1 |
a002 | 3 |
Example analysis
The order number a002 Yes 3 Line order record is also the largest order number, so it is returned in the last place , Orders with the same number of order lines do not need to be processed too much .
Example
1 2 3 4 5 |
|
answer
Investigate knowledge points :
COUNT( Name )
: Returns the number of values for the specified column .AS
: Take the alias .GROUP BY
: Groups rows according to the value of the specified column or expression .ORDER BY
: According to the following column names Sort ,ASC
Indicate positive order , Also by default. Sort ,DESC
Reverse order .
1 |
|
SQL28 The lowest cost product per supplier
describe
Yes Products surface , Containing fields prod_price Represents the product price ,vend_id On behalf of the supplier id
vend_id | prod_price |
---|---|
a0011 | 100 |
a0019 | 0.1 |
b0019 | 1000 |
b0019 | 6980 |
b0019 | 20 |
problem
To write SQL sentence , Return a file named cheapest_item Field of , This field contains the products with the lowest cost per supplier ( Use Products In the table prod_price), Then the results are sorted in ascending order from the lowest cost to the highest cost Sort .
Sample results
Return to supplier id vend_id And the lowest cost products of corresponding suppliers cheapest_item.
vend_id | cheapest_item |
---|---|
a0019 | 0.1 |
b0019 | 20 |
a0011 | 100 |
Example analysis
for example b0019 The lowest cost price is 20, And finally according to *** grid Sort The return sequence is a0019、b0019、a0011.
Example
1 2 3 4 5 6 7 8 9 10 |
|
answer
To find the lowest cost products among various suppliers , You need to pass the keyword GROUP BY
To group , Then with the help of the function MIN()
find prod_price
The smallest value in , Then alias it cheapest_item
, Finally, according to the lowest cost products among the suppliers cheapest_item
Using keywords ORDER BY
Ascending order Sort .
1 |
|
SQL29 The total number of returned orders shall not be less than 100 The order number of all orders for
describe
OrderItems Represents the order item table , Include : The order number order_num And order quantity quantity.
order_num | quantity |
---|---|
a1 | 105 |
a2 | 1100 |
a2 | 200 |
a4 | 1121 |
a5 | 10 |
a2 | 19 |
a7 | 5 |
problem
Please write SQL sentence , The total number of returned orders shall not be less than 100 All order numbers for , The final result is in ascending order according to the order number Sort .
Sample results
return order_num The order number .
order_num |
---|
a1 |
a2 |
a4 |
Example analysis
The order number a1、a2、a4 Of quantity The sum is greater than or equal to 100, In order a1、a2、a4.
Example
1 2 3 4 5 6 |
|
answer
Conditions of the query , Only at this time, it is no longer filtering the specified line , Instead, you need to filter groups , So you can't use keywords at this time WHERE
, Instead, you need to use keywords HAVING
, It's usually with keywords GROUP BY
Continuous use . In addition, you should pay attention to the order of keywords , First, GROUP BY
, Followed by the HAVING
, The last is ORDER BY
.
1 |
|
SQL30 Calculate the sum
describe
OrderItems Table represents order information , Including field : The order number order_num and item_price The selling price of the goods 、quantity The number .
order_num | item_price | quantity |
---|---|---|
a1 | 10 | 105 |
a2 | 1 | 1100 |
a2 | 1 | 200 |
a4 | 2 | 1121 |
a5 | 5 | 10 |
a2 | 1 | 19 |
a7 | 7 | 5 |
problem
To write SQL sentence , Aggregate by order number , The total price of the returned order shall not be less than 1000 All order numbers for , The final results are in ascending order according to the order number Sort .
Tips : The total price = item_price multiply quantity
Sample results
order_num | total_price |
---|---|
a1 | 1050 |
a2 | 1319 |
a4 | 2242 |
Example
1 2 3 4 5 6 7 |
|
answer
The above questions have already covered the knowledge points , This question is just a comprehensive application , The main contents are as follows :
SUM()
: Sum the total price of the same product .AS
: Take the alias .GROUP BY
: Group by column .HAVING
: AndGROUP BY
Combined use to achieve conditional filtering .ORDER BY
: Proceed in columns Sort .
1 |
|
边栏推荐
- 学习记录十
- One bite of Stream(5)
- Graduation thesis topics of hydrology and water resources [213]
- 二、mysql进程之间关系、配置文件、文件socket、网络socket、mysql密码破解
- 学习记录十三
- Combined with the source code, see "cocos2dx-3.0 as I understand" - particle system
- 请教一下,有没有比较好的方式做数据库的迁移,整库迁移,从mysql(旧)-->mysql(新)?
- hello csdn
- Understanding and using unity2d custom scriptable tiles (I) -- understanding the tilebase class
- WordPress opens sitemap XML, a piece of code to solve.
猜你喜欢
解决错误:Error: TomEE required to support EAR/EJB deployment
学习记录十三
SAFERTOS在医疗设备中的应用
Uni app - plugin [app cloud packaging] installation failed! (the app cloud packaging plug-in installation fails when the app is packaged) solution
UGUI官方优化文档翻译
学习笔记-浅谈MySql索引的数据结构与算法和索引的介绍
香蕉派 BPI-M5折腾记录(2)—— 编译u-boot
不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第四篇)
不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第五篇)
Understanding and use of unity2d custom scriptable tiles (III) -- start building a custom tile based on the tile class (Part 1)
随机推荐
UGUI——RectMask2D
02-线性结构4 Pop Sequence(入栈,出栈的模拟)
"Dry goods experiment" Huawei DHCP + single arm routing experiment
TensorFlow的学习笔记(一)
jenkins设置语言为中文
UGUI官方优化文档翻译
剑指offer专项突击版第4天
Sword finger offer special assault version day 3
剑指offer专项突击版第3天
MiNi简约版小程序送上,专栏用户独享
离线安装jenkins及离线安装插件
Experience of driver development for embedded devices and external devices
[Unity脚本优化]Optimizing scripts in Unity games——2019以下
Understanding and use of unity2d custom scriptable tiles (II) -- understand the methods of the tilebase class (mainly gettiledata method) in detail by understanding the tile class
JupyterNotebook插件管理与安装
[Unity脚本优化] Optimizing garbage collection in Unity games
聪明人的游戏提高篇:第三章第一课:grid(格子位置)
Mini simple version applet is delivered, which is exclusive to column users
JS构造链表
【深度学习】使用yolov5对数据进行预标注