SQL131. 作答试卷得分大于过80的人的用户等级分布
描述
id | uid | nick_name | achievement | level | job | register_time |
1 | 1001 | 牛客1号 | 3100 | 7 | 算法 | 2020-01-01 10:00:00 |
2 | 1002 | 牛客2号 | 2100 | 6 | 算法 | 2020-01-01 10:00:00 |
3 | 1003 | 牛客3号 | 1500 | 5 | 算法 | 2020-01-01 10:00:00 |
4 | 1004 | 牛客4号 | 1100 | 4 | 算法 | 2020-01-01 10:00:00 |
5 | 1005 | 牛客5号 | 1600 | 6 | C++ | 2020-01-01 10:00:00 |
6 | 1006 | 牛客6号 | 3000 | 6 | C++ | 2020-01-01 10:00:00 |
id | exam_id | tag | difficulty | duration | release_time |
1 | 9001 | SQL | hard | 60 | 2021-09-01 06:00:00 |
2 | 9002 | C++ | easy | 60 | 2021-09-01 06:00:00 |
3 | 9003 | 算法 | medium | 80 | 2021-09-01 10:00:00 |
id | uid | exam_id | start_time | submit_time | score |
1 | 1001 | 9001 | 2021-09-01 09:01:01 | 2021-09-01 09:41:01 | 79 |
2 | 1002 | 9003 | 2021-09-01 12:01:01 | 2021-09-01 12:21:01 | 60 |
3 | 1002 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:01 | 70 |
4 | 1002 | 9001 | 2021-09-01 19:01:01 | 2021-09-01 19:40:01 | 80 |
5 | 1002 | 9003 | 2021-08-01 12:01:01 | 2021-08-01 12:21:01 | 60 |
6 | 1002 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:01 | 70 |
7 | 1002 | 9001 | 2021-09-01 19:01:01 | 2021-09-01 19:40:01 | 85 |
8 | 1002 | 9002 | 2021-09-01 12:01:01 | (NULL) | (NULL) |
9 | 1003 | 9003 | 2021-09-07 10:01:01 | 2021-09-07 10:31:01 | 86 |
10 | 1003 | 9003 | 2021-09-08 12:01:01 | 2021-09-08 12:11:01 | 40 |
11 | 1003 | 9001 | 2021-09-01 13:01:01 | 2021-09-01 13:41:01 | 81 |
12 | 1003 | 9002 | 2021-09-01 14:01:01 | (NULL) | (NULL) |
13 | 1003 | 9003 | 2021-09-08 15:01:01 | (NULL) | (NULL) |
14 | 1005 | 9001 | 2021-09-01 12:01:01 | 2021-09-01 12:31:01 | 90 |
15 | 1005 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:01 | 88 |
16 | 1006 | 9002 | 2021-09-01 12:11:01 | 2021-09-01 12:31:01 | 89 |
level | level_cnt |
6 | 2 |
5 | 1 |
示例1
输入:
drop table if exists examination_info; CREATE TABLE examination_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', exam_id int UNIQUE NOT NULL COMMENT '试卷ID', tag varchar(32) COMMENT '类别标签', difficulty varchar(8) COMMENT '难度', duration int NOT NULL COMMENT '时长', release_time datetime COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; drop table if exists user_info; CREATE TABLE user_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int UNIQUE NOT NULL COMMENT '用户ID', `nick_name` varchar(64) COMMENT '昵称', achievement int COMMENT '成就值', level int COMMENT '用户等级', job varchar(32) COMMENT '职业方向', register_time datetime COMMENT '注册时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; drop table if exists exam_record; CREATE TABLE exam_record ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int NOT NULL COMMENT '用户ID', exam_id int NOT NULL COMMENT '试卷ID', start_time datetime NOT NULL COMMENT '开始时间', submit_time datetime COMMENT '提交时间', score tinyint COMMENT '得分' )CHARACTER SET utf8 COLLATE utf8_general_ci; INSERT INTO user_info(uid,`nick_name`,achievement,level,job,register_time) VALUES (1001, '牛客1号', 3100, 7, '算法', '2020-01-01 10:00:00'), (1002, '牛客2号', 2100, 6, '算法', '2020-01-01 10:00:00'), (1003, '牛客3号', 1500, 5, '算法', '2020-01-01 10:00:00'), (1004, '牛客4号', 1100, 4, '算法', '2020-01-01 10:00:00'), (1005, '牛客5号', 1600, 6, 'C++', '2020-01-01 10:00:00'), (1006, '牛客6号', 3000, 6, 'C++', '2020-01-01 10:00:00'); INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES (9001, 'SQL', 'hard', 60, '2021-09-01 06:00:00'), (9002, 'C++', 'easy', 60, '2021-09-01 06:00:00'), (9003, '算法', 'medium', 80, '2021-09-01 10:00:00'); INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES (1001, 9001, '2021-09-01 09:01:01', '2021-09-01 09:41:01', 79), (1002, 9003, '2021-09-01 12:01:01', '2021-09-01 12:21:01', 60), (1002, 9002, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 70), (1002, 9001, '2021-09-01 19:01:01', '2021-09-01 19:40:01', 80), (1002, 9003, '2021-08-01 12:01:01', '2021-08-01 12:21:01', 60), (1002, 9002, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 70), (1002, 9001, '2021-09-01 19:01:01', '2021-09-01 19:40:01', 85), (1002, 9002, '2021-09-01 12:01:01', null, null), (1003, 9003, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 86), (1003, 9003, '2021-09-08 12:01:01', '2021-09-08 12:11:01', 40), (1003, 9001, '2021-09-01 13:01:01', '2021-09-01 13:41:01', 81), (1003, 9002, '2021-09-01 14:01:01', null, null), (1003, 9003, '2021-09-08 15:01:01', null, null), (1005, 9001, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 90), (1005, 9002, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88), (1006, 9002, '2021-09-01 12:11:01', '2021-09-01 12:31:01', 89);
输出:
6|2 5|1
Mysql 解法, 执行用时: 36ms, 内存消耗: 6408KB, 提交时间: 2022-01-03
SELECT level, COUNT(DISTINCT uid) AS level_cnt FROM exam_record er JOIN user_info ui USING(uid) JOIN examination_info ei USING(exam_id) WHERE ei.tag='SQL' AND er.score > 80 GROUP BY level ORDER BY COUNT(DISTINCT uid) DESC, level DESC
Mysql 解法, 执行用时: 36ms, 内存消耗: 6520KB, 提交时间: 2021-12-07
select ui.level,count(ui.level) as level_cnt from exam_record as er join user_info as ui using(uid) where er.exam_id in (select exam_id FROM examination_info where tag='SQL') and er.score>80 group by ui.level order by level_cnt desc
Mysql 解法, 执行用时: 37ms, 内存消耗: 6328KB, 提交时间: 2021-12-08
SELECT level, count(distinct uid) as level_cnt FROM exam_record JOIN examination_info USING (exam_id) JOIN user_info USING (uid) where score > 80 and tag='SQL' GROUP BY level ORDER BY count(DISTINCT uid) DESC, level DESC
Mysql 解法, 执行用时: 37ms, 内存消耗: 6348KB, 提交时间: 2022-01-24
select ui.level as 'level', count(*) as level_cnt from exam_record as er join examination_info as ei on er.exam_id = ei.exam_id join user_info as ui on ui.uid = er.uid where ei.tag = 'SQL' and er.score > 80 group by ui.level order by level_cnt desc;
Mysql 解法, 执行用时: 37ms, 内存消耗: 6384KB, 提交时间: 2021-12-02
select level, count(distinct u_i.uid) as level_cnt from exam_record e_r join examination_info e_i on e_r.exam_id = e_i.exam_id join user_info u_i on e_r.uid = u_i.uid where tag = 'SQL' and score > 80 group by level order by level_cnt desc, level desc