当前位置:网站首页>Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
2022-07-28 13:45:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (062)—— MySQL Exercises : Operation questions 32-38( 6、 ... and )
32、 Subquery
subject : Query the minimum of each school gpa, Output results by university Ascending order .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | female | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
The query should return the following results :
| device_id | university | gpa |
|---|---|---|
| 6543 | Peking University, | 3.2000 |
| 4321 | Fudan University | 3.6000 |
| 2131 | Shandong University | 3.3000 |
| 2315 | Zhejiang University | 3.6000 |
Table structure and data are as follows :
/* drop table if exists user_profile; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',3.6,9,6,52); */
answer :
/* select a.device_id, b.university, round(b.gpa,4) gpa from user_profile a join (select university, min(gpa) gpa from user_profile group by university) b on a.gpa = b.gpa and a.university = b.university order by a.university; */
mysql> select a.device_id, b.university, round(b.gpa,4) gpa
-> from user_profile a join
-> (select university, min(gpa) gpa
-> from user_profile group by university) b
-> on a.gpa = b.gpa and a.university = b.university
-> order by a.university;
+-----------+--------------+--------+
| device_id | university | gpa |
+-----------+--------------+--------+
| 6543 | Peking University, | 3.2000 |
| 4321 | Fudan University | 3.6000 |
| 2131 | Shandong University | 3.3000 |
| 2315 | Zhejiang University | 3.6000 |
+-----------+--------------+--------+
4 rows in set (0.00 sec)
33、 Group query and statistics (1)
subject : Query every user of Fudan University in 8 The number of questions practiced in the month and the number of questions answered correctly , Take out the corresponding detailed data . For in 8 Users who haven't practiced in this month , Answer number result return 0.
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 |
|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 |
| 2 | 3214 | male | Fudan University | 4.0 | 15 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 |
| 7 | 4321 | female | 26 | Fudan University | 3.6 | 9 |
Example :question_practice_detail The data in the table are as follows .
| id | device_id | question_id | result | date |
|---|---|---|---|---|
| 1 | 2138 | 111 | wrong | 2021-05-03 |
| 2 | 3214 | 112 | wrong | 2021-05-09 |
| 3 | 3214 | 113 | wrong | 2021-06-15 |
| 4 | 6543 | 111 | right | 2021-08-13 |
| 5 | 2315 | 115 | right | 2021-08-13 |
| 6 | 2315 | 116 | right | 2021-08-14 |
| 7 | 2315 | 117 | wrong | 2021-08-15 |
| …… |
The query should return the following results :
| device_id | university | question_cnt | right_question_cnt |
|---|---|---|---|
| 3214 | Fudan University | 3 | 0 |
| 4321 | Fudan University | 0 | 0 |
Table structure and data are as follows :
/* drop table if exists `user_profile`; drop table if exists `question_practice_detail`; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int ); CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL, `date` date NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4,7); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20); INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15); INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',3.6,9); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16'); INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18'); INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13'); */
answer :
/* select u.device_id, u.university, count(question_id) question_cnt, sum(if(q.result = 'right', 1, 0)) right_question_cnt from user_profile u left join question_practice_detail q on u.device_id = q.device_id where u.university = ' Fudan University ' and (month(q.date) = 8 or month(q.date) is null) group by u.device_id, u.university; */
mysql> select u.device_id, u.university,
-> count(question_id) question_cnt,
-> sum(if(q.result = 'right', 1, 0)) right_question_cnt
-> from user_profile u left join question_practice_detail q
-> on u.device_id = q.device_id
-> where u.university = ' Fudan University ' and (month(q.date) = 8 or month(q.date) is null)
-> group by u.device_id, u.university;
+-----------+--------------+--------------+--------------------+
| device_id | university | question_cnt | right_question_cnt |
+-----------+--------------+--------------+--------------------+
| 3214 | Fudan University | 3 | 0 |
| 4321 | Fudan University | 0 | 0 |
+-----------+--------------+--------------+--------------------+
2 rows in set (0.00 sec)
/*
explain :
(1)count(question_id): Not available here count(*), because count(*) Statistics are all records , And of course question_id Empty record ,count(question_id) Only statistics question_id Not empty record
(2)where Conditions must be specified :month(q.date) is null, Otherwise, even if you use left join, Records that do not meet the connection conditions will also be filtered
34、 Group query and statistics (2)
subject : Query the accuracy of users of Zhejiang University in answering different difficult questions , Take out the corresponding data and output it in ascending order of accuracy .
Example : user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | Fudan University | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | Peking University, | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | Shandong University | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | female | 26 | Fudan University | 3.6 | 9 | 6 | 52 |
Example : question_practice_detail The data in the table are as follows .
| id | device_id | question_id | result |
|---|---|---|---|
| 1 | 2138 | 111 | wrong |
| 2 | 3214 | 112 | wrong |
| 3 | 3214 | 113 | wrong |
| 4 | 6543 | 111 | right |
| 5 | 2315 | 115 | right |
| 6 | 2315 | 116 | right |
| 7 | 2315 | 117 | wrong |
Example : question_detail The data in the table are as follows .
| question_id | difficult_level |
|---|---|
| 111 | hard |
| 112 | medium |
| 113 | easy |
| 115 | easy |
| 116 | medium |
| 117 | easy |
The query should return the following results :
| difficult_level | correct_rate |
|---|---|
| easy | 0.5000 |
| medium | 1.0000 |
Table structure and data are as follows :
/* drop table if exists `user_profile`; drop table if exists `question_practice_detail`; drop table if exists `question_detail`; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); CREATE TABLE `question_practice_detail` ( `id` int NOT NULL, `device_id` int NOT NULL, `question_id`int NOT NULL, `result` varchar(32) NOT NULL, `date` date NOT NULL ); CREATE TABLE `question_detail` ( `id` int NOT NULL, `question_id`int NOT NULL, `difficult_level` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',3.6,9,6,52); INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03'); INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15'); INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09'); INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13'); INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14'); INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15'); INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16'); INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18'); INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13'); INSERT INTO question_detail VALUES(1,111,'hard'); INSERT INTO question_detail VALUES(2,112,'medium'); INSERT INTO question_detail VALUES(3,113,'easy'); INSERT INTO question_detail VALUES(4,115,'easy'); INSERT INTO question_detail VALUES(5,116,'medium'); INSERT INTO question_detail VALUES(6,117,'easy'); */
answer :
/* select b.difficult_level, round(sum(if(result = 'right', 1, 0))/count(*), 4) correct_rate from question_practice_detail a join question_detail b on a.question_id = b.question_id where a.device_id in (select device_id from user_profile where university = ' Zhejiang University ') group by b.difficult_level order by correct_rate; */
mysql> select b.difficult_level,
-> round(sum(if(result = 'right', 1, 0))/count(*), 4) correct_rate
-> from question_practice_detail a join question_detail b
-> on a.question_id = b.question_id
-> where a.device_id in (select device_id from user_profile where university = ' Zhejiang University ')
-> group by b.difficult_level
-> order by correct_rate;
+-----------------+--------------+
| difficult_level | correct_rate |
+-----------------+--------------+
| easy | 0.5000 |
| medium | 1.0000 |
+-----------------+--------------+
2 rows in set (0.00 sec)
35、 Sort query results (1)
subject : Query the user age in the user information table , Take out the corresponding data and sort them in ascending order of age .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 |
| 2 | 3214 | male | 23 | Fudan University | 4 |
| 3 | 6543 | female | 20 | Peking University, | 3.2 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 |
| 6 | 2131 | male | 28 | Beijing Normal University | 3.3 |
The query should return the following results :
| device_id | age |
|---|---|
| 6534 | 20 |
| 2138 | 21 |
| 3214 | 23 |
| 2315 | 23 |
| 5432 | 25 |
| 2131 | 28 |
Table structure and data are as follows :
/* drop table if exists user_profile; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float); INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ',3.4); INSERT INTO user_profile VALUES(2,3214,'male',23,' Fudan University ',4.0); INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2); INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6); INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8); INSERT INTO user_profile VALUES(6,2131,'male',28,' Beijing Normal University ',3.3); */
answer :
mysql> select device_id, age from user_profile order by age;
+-----------+------+
| device_id | age |
+-----------+------+
| 6543 | 20 |
| 2138 | 21 |
| 3214 | 23 |
| 2315 | 23 |
| 5432 | 25 |
| 2131 | 28 |
+-----------+------+
6 rows in set (0.00 sec)
36、 Sort query results (1)
subject : Query the age and... In the user information table gpa data , And first according to gpa Ascending sort , Then sort the output in ascending order of age .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 |
| 2 | 3214 | male | 23 | Fudan University | 4 |
| 3 | 6543 | female | 20 | Peking University, | 3.2 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 |
| 6 | 2131 | male | 28 | Beijing Normal University | 3.3 |
The query should return the following results :
| device_id | gpa | age |
|---|---|---|
| 6543 | 3.2 | 20 |
| 2131 | 3.3 | 28 |
| 2138 | 3.4 | 21 |
| 2315 | 3.6 | 23 |
| 5432 | 3.8 | 25 |
| 3214 | 4 | 23 |
answer :
mysql> select device_id, gpa, age from user_profile order by gpa, age;
+-----------+------+------+
| device_id | gpa | age |
+-----------+------+------+
| 6543 | 3.2 | 20 |
| 2131 | 3.3 | 28 |
| 2138 | 3.4 | 21 |
| 2315 | 3.6 | 23 |
| 5432 | 3.8 | 25 |
| 3214 | 4 | 23 |
+-----------+------+------+
6 rows in set (0.00 sec)
37、 Sort query results (3)
subject : Query the corresponding data in the user information table , And first according to gpa Descending 、 Sort the output in descending order of age .
Example :user_profile The data in the table are as follows .
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | Peking University, | 3.4 |
| 2 | 3214 | male | 23 | Fudan University | 4 |
| 3 | 6543 | female | 20 | Peking University, | 3.2 |
| 4 | 2315 | female | 23 | Zhejiang University | 3.6 |
| 5 | 5432 | male | 25 | Shandong University | 3.8 |
| 6 | 2131 | male | 28 | Beijing Normal University | 3.3 |
The query should return the following results :
| device_id | gpa | age |
|---|---|---|
| 3214 | 4 | 23 |
| 5432 | 3.8 | 25 |
| 2315 | 3.6 | 23 |
| 2138 | 3.4 | 21 |
| 2131 | 3.3 | 28 |
| 6543 | 3.2 | 20 |
answer :
mysql> select device_id, gpa, age from user_profile order by gpa desc, age desc;
+-----------+------+------+
| device_id | gpa | age |
+-----------+------+------+
| 3214 | 4 | 23 |
| 5432 | 3.8 | 25 |
| 2315 | 3.6 | 23 |
| 2138 | 3.4 | 21 |
| 2131 | 3.3 | 28 |
| 6543 | 3.2 | 20 |
+-----------+------+------+
6 rows in set (0.00 sec)
38、 The use of aggregate functions
subject : Inquire about 2021 year 8 The total number of users who have practiced the topic and the total number of times they have practiced the topic in the month , Take out the corresponding results .
Example :question_practice_detail The data in the table are as follows .
| id | device_id | question_id | result | date |
|---|---|---|---|---|
| 1 | 2138 | 111 | wrong | 2021-05-03 |
| 2 | 3214 | 112 | wrong | 2021-05-09 |
| 3 | 3214 | 113 | wrong | 2021-06-15 |
| 4 | 6543 | 111 | right | 2021-08-13 |
| 5 | 2315 | 115 | right | 2021-08-13 |
| 6 | 2315 | 116 | right | 2021-08-14 |
| 7 | 2315 | 117 | wrong | 2021-08-15 |
| …… |
The query should return the following results :
| did_cnt | question_cnt |
|---|---|
| 3 | 12 |
answer :
/* select count(distinct device_id) did_cnt, count(*) question_cnt from question_practice_detail where date between '2021-8-1' and '2021-8-31'; */
mysql> select count(distinct device_id) did_cnt,
-> count(*) question_cnt
-> from question_practice_detail
-> where date between '2021-8-1' and '2021-8-31';
+---------+--------------+
| did_cnt | question_cnt |
+---------+--------------+
| 3 | 12 |
+---------+--------------+
1 row in set (0.02 sec)
边栏推荐
- Leetcode · daily question · 1331. array sequence number conversion · discretization
- 数据库系统原理与应用教程(059)—— MySQL 练习题:操作题 1-10(三)
- 少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月
- Map tiles: detailed explanation of vector tiles and grid tiles
- 使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
- C language: merge sort
- JWT login authentication + token automatic renewal scheme, well written!
- Debezium系列之:2.0.0.Beta1的重大变化和新特性
- Beyond istio OSS -- current situation and future of istio Service Grid
- I miss the year of "losing" Li Ziqi
猜你喜欢

酷炫操作预热!代码实现小星球特效

屈辱、抗争、逆转,三十年,中国该赢微软一次了

30天刷题计划(三)

Half wave rectification light LED

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development

Beyond Istio OSS——Istio服务网格的现状与未来

严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数

【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...

jar包

SQL每日一练(牛客新题库)——第4天:高级操作符
随机推荐
C语言:随机生成数+归并排序
SQL daily practice (Niuke new question bank) - day 4: advanced operators
30天刷题计划(二)
Today's sleep quality record 75 points
记一次使用pdfbox解析pdf,获取pdf的关键数据的工具使用
Jenkins--持续集成服务器
功率放大器和匹配网络学习
Dry goods -- encapsulated anti shake and throttling method in the project
蓝桥集训(附加面试题)第七天
无法连接服务器怎么办(原始服务器找不到目标资源)
Some thoughts on.Net desktop development
111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
DOJP1520星门跳跃题解
30天刷题训练(一)
POJ3268最短路径题解
Denial of service DDoS Attacks
C语言:顺序存储结构的快速排序
PHP generates random numbers (nickname random generator)
Leetcode notes 566. Reshaping the matrix
[ecmascript6] symbol and its related use