当前位置:网站首页>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)
边栏推荐
猜你喜欢

沾上趣店,都得道歉?

Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open

30天刷题计划(二)

.NET桌面开发的一些思考

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

Guide for using IP phone system and VoIP system

面经整理,助力秋招,祝你称为offer收割机

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response

30天刷题训练(一)

SQL每日一练(牛客新题库)——第4天:高级操作符
随机推荐
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
SQL每日一练(牛客新题库)——第4天:高级操作符
POJ3268最短路径题解
I'm bald! Who should I choose for unique index or general index?
要想组建敏捷团队,这些方法不可少
面经整理,助力秋招,祝你称为offer收割机
PHP生成随机数(昵称随机生成器)
C语言:优化后的归并排序
不用Swagger,那我用啥?
paddleClas分类实践记录
POJ3259虫洞题解
Realize the mutual value transfer between main window and sub window in WPF
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
PHP generates random numbers (nickname random generator)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
酷炫操作预热!代码实现小星球特效
Beyond istio OSS -- current situation and future of istio Service Grid
微念“失去”李子柒的这一年
【架构】评分较高的三本微服务书籍的阅读笔记
Some thoughts on.Net desktop development