当前位置:网站首页>数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
2022-07-28 12:30:00 【睿思达DBA_WGX】
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
32、子查询
题目:查询每个学校的最低 gpa,输出结果按 university 升序排列。
示例:user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | 复旦大学 | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | female | 26 | 复旦大学 | 3.6 | 9 | 6 | 52 |
查询应返回以下结果:
| device_id | university | gpa |
|---|---|---|
| 6543 | 北京大学 | 3.2000 |
| 4321 | 复旦大学 | 3.6000 |
| 2131 | 山东大学 | 3.3000 |
| 2315 | 浙江大学 | 3.6000 |
表结构及数据如下:
/* 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,'北京大学',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52); */
解答:
/* 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 | 北京大学 | 3.2000 |
| 4321 | 复旦大学 | 3.6000 |
| 2131 | 山东大学 | 3.3000 |
| 2315 | 浙江大学 | 3.6000 |
+-----------+--------------+--------+
4 rows in set (0.00 sec)
33、分组查询与统计(1)
题目: 查询复旦大学的每个用户在 8 月份练习的题目数和回答正确的题目数,取出相应的明细数据。对于在 8 月份没有练习过的用户,答题数结果返回 0。
示例:user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa | active_days_within_30 |
|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 |
| 2 | 3214 | male | 复旦大学 | 4.0 | 15 | |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 |
| 6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 |
| 7 | 4321 | female | 26 | 复旦大学 | 3.6 | 9 |
示例:question_practice_detail 表的数据如下。
| 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 |
| …… |
查询应返回以下结果:
| device_id | university | question_cnt | right_question_cnt |
|---|---|---|---|
| 3214 | 复旦大学 | 3 | 0 |
| 4321 | 复旦大学 | 0 | 0 |
表结构及数据如下:
/* 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,'北京大学',3.4,7); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',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'); */
解答:
/* 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 = '复旦大学' 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 = '复旦大学' 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 | 复旦大学 | 3 | 0 |
| 4321 | 复旦大学 | 0 | 0 |
+-----------+--------------+--------------+--------------------+
2 rows in set (0.00 sec)
/*
说明:
(1)count(question_id):此处不能使用 count(*),因为 count(*) 统计的是所有记录,当然也包括 question_id 为空的记录,count(question_id) 只统计 question_id 不为空的记录
(2)where 必须指定条件:month(q.date) is null,否则即使使用了 left join,不满足连接条件的记录也会被过滤掉
34、分组查询与统计(2)
题目:查询浙江大学的用户在不同难度题目下答题的正确率情况,取出相应数据并按照准确率升序输出。
示例: user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | 复旦大学 | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | female | 26 | 复旦大学 | 3.6 | 9 | 6 | 52 |
示例: question_practice_detail 表的数据如下。
| 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 |
示例: question_detail 表的数据如下。
| question_id | difficult_level |
|---|---|
| 111 | hard |
| 112 | medium |
| 113 | easy |
| 115 | easy |
| 116 | medium |
| 117 | easy |
查询应返回以下结果:
| difficult_level | correct_rate |
|---|---|
| easy | 0.5000 |
| medium | 1.0000 |
表结构及数据如下:
/* 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,'北京大学',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',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'); */
解答:
/* 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 = '浙江大学') 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 = '浙江大学')
-> 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、对查询结果排序(1)
题目:查询用户信息表中的用户年龄,取出相应数据并按照年龄升序排序。
示例:user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 |
| 2 | 3214 | male | 23 | 复旦大学 | 4 |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 |
| 6 | 2131 | male | 28 | 北京师范大学 | 3.3 |
查询应返回以下结果:
| device_id | age |
|---|---|
| 6534 | 20 |
| 2138 | 21 |
| 3214 | 23 |
| 2315 | 23 |
| 5432 | 25 |
| 2131 | 28 |
表结构及数据如下:
/* 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,'北京大学',3.4); INSERT INTO user_profile VALUES(2,3214,'male',23,'复旦大学',4.0); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8); INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3); */
解答:
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、对查询结果排序(1)
题目:查询用户信息表中的年龄和 gpa 数据,并且先按照 gpa 升序排序,再按照年龄升序排序输出。
示例:user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 |
| 2 | 3214 | male | 23 | 复旦大学 | 4 |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 |
| 6 | 2131 | male | 28 | 北京师范大学 | 3.3 |
查询应返回以下结果:
| 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 |
解答:
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、对查询结果排序(3)
题目:查询用户信息表中对应的数据,并且先按照 gpa 降序、年龄降序排序输出。
示例:user_profile 表的数据如下。
| id | device_id | gender | age | university | gpa |
|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 |
| 2 | 3214 | male | 23 | 复旦大学 | 4 |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 |
| 6 | 2131 | male | 28 | 北京师范大学 | 3.3 |
查询应返回以下结果:
| 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 |
解答:
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、聚合函数的使用
题目:查询 2021 年 8月份所有练习过题目的总用户数和练习过题目的总次数,取出相应结果。
示例:question_practice_detail 表的数据如下。
| 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 |
| …… |
查询应返回以下结果:
| did_cnt | question_cnt |
|---|---|
| 3 | 12 |
解答:
/* 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 笔记 118. 杨辉三角
- 【C语言】结构体指针与结构体变量作形参的区别
- Dry goods -- encapsulated anti shake and throttling method in the project
- Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
- 沾上趣店,都得道歉?
- 2020jenkins study notes
- Deployment之滚动更新策略。
- Definition of option basis
- [FPGA]: AD sampling problem
- 无法连接服务器怎么办(原始服务器找不到目标资源)
猜你喜欢

PCP parity principle arbitrage

拥有游戏的一部分,写在我的世界禁用NFT之后

Have a part of the game, after NFT is disabled in my world

FFT海浪模拟

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

Go language - Application of stack - expression evaluation

Using auto.js to realize fifaol3 brush teaching assistant

Can second uncle cure young people's spiritual internal friction?
![[matlab]: FFT related problems](/img/08/c721394c172bdfd1d5d55aef31ccbf.png)
[matlab]: FFT related problems

屈辱、抗争、逆转,三十年,中国该赢微软一次了
随机推荐
10、 Kubernetes scheduling principle
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
Basic exercises of DQL in MySQL
PCP parity principle arbitrage
Night God simulator packet capturing wechat applet
Three men "running away" from high positions in the mobile phone factory
8、 Kubernetes network and load balancing
Using auto.js to realize fifaol3 brush teaching assistant
Single option trading and cross trade
docker部署mysql 实现远程连接[通俗易懂]
我秃了!唯一索引、普通索引我该选谁?
Leetcode notes 118. Yang Hui triangle
Rust 从入门到精通01-简介
一根筋教育PHP培训 知行合一收热捧
powerdesigner创建数据库模型(概念模型举例)
kotlin学习笔记3——Lambda编程
Chapter 6 提升
[FPGA]: AD sampling problem
为什么说Crypto游戏正在改变游戏产业?
Table list filter results remain unchanged