PTA—sql补题(4)

目录

找出每个学校GPA最低的同学

表结构:

表样例

输出样例:

查找当前薪水详情以及部门编号dept_no

描述

作答试卷得分大于过80的人的用户等级分布

描述

试卷发布当天作答人数和平均分

表结构:

表样例

输出样例:

 筛选限定昵称成就值活跃日期的用户

描述


找出每个学校GPA最低的同学

现在运营想要找到每个学校gpa最低的同学来做调研,请你取出每个学校的最低gpa。

提示:请使用SELECT语句作答。

表结构:

请在这里写定义表结构的SQL语句。例如:


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);

表样例

请在这里给出上述表结构对应的表样例。例如

user_profile表:

输出样例:

请在这里给出输出样例。例如:

6543|北京大学|3.200

4321|复旦大学|3.600

2131|山东大学|3.300

2315|浙江大学|3.600

  • 限定条件:gpa最低,看似min(gpa),但是要留意,是每个学校里的最低,不是全局最低。min(gpa)的时候对应同学的ID丢了,直接干是拿不到最低gpa对应的同学ID的;
  • 用group by把学校分组,然后计算得到每个学校最低gpa,再去找这个学校里和这个gpa相等的同学ID。注意这样如果最低gpa对应多个同学,都会输出,题目没有明确此种情况,心理明白就行。
SELECT device_id,university,gpa
FROM user_profile
WHERE (university,gpa) IN (SELECT university, min(gpa)
                FROM user_profile
                GROUP BY university)
ORDER BY university

查找当前薪水详情以及部门编号dept_no

描述

有一个全部员工的薪水表salaries简况如下:

emp_no salary from_date to_date
10001 88958 2002-06-22 9999-01-01
10002 72527 2001-08-02 9999-01-01
10003 43311 2001-12-01 9999-01-01

有一个各个部门的领导表dept_manager简况如下:

dept_no emp_no to_date
d001 10001 9999-01-01
d002 10003 9999-01-01

请你查找各个部门当前领导的薪水详情以及其对应部门编号dept_no,输出结果以salaries.emp_no升序排序,并且请注意输出结果里面dept_no列是最后一列,以上例子输出如下:

emp_no salary from_date to_date dept_no
10001 88958 2002-06-22 9999-01-01 d001
10003 43311 2001-12-01 9999-01-01 d002
select d.emp_no,s.salary,s.from_date,s.to_date,d.dept_no
from dept_manager d left join salaries s on d.emp_no=s.emp_no
order by s.emp_no

作答试卷得分大于过80的人的用户等级分布

描述

现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):

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

试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间):

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

试卷作答信息表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

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

统计作答SQL类别的试卷得分大于过80的人的用户等级分布,按数量降序排序(保证数量都不同)。示例数据结果输出如下:

level level_cnt
6 2
5 1

解释:9001为SQL类试卷,作答该试卷大于80分的人有1002、1003、1005共3人,6级两人,5级一人。

思路:

  • 按等级分组,计算每个等级的符合条件的人数。知识点:group by
  • 只挑选类别为SQL、且得分大于80的不同的用户进行统计人数,相同的用户只统计一次:
  • 按照人数的降序,相同情况下等级降序输出。order by level_cnt desc, level desc
select level,count(*) level_cnt
from user_info
where uid in(select uid 
            from exam_record er join examination_info ei on er.exam_id=ei.exam_id
            where score>80
            and tag='SQL')
group by level
order by level_cnt desc

试卷发布当天作答人数和平均分

请计算每张SQL类别试卷发布后,当天5级以上的用户作答的人数uv和平均分avg_score,按人数降序,相同人数的按平均分升序。

提示:请使用SELECT语句作答。

表结构:

drop table if exists examination_info,user_info,exam_record;
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;

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;

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;

表样例

user_info表:

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

输出样例:

exam_id uv avg_score
9001 3 81.3
select er.exam_id, count(distinct er.uid) uv,round(avg(er.score),1) avg_score
from examination_info ei,exam_record er
where ei.exam_id=er.exam_id
and ei.tag='SQL'
and date_format(ei.release_time,'%Y%m%d')=date_format(er.start_time,'%Y%m%d')
and er.uid in(select uid
             from user_info
             where level>5)
group by er.exam_id
order by uv desc,avg_score asc

 筛选限定昵称成就值活跃日期的用户

描述

现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):

id uid nick_name achievement level job register_time
1 1001 牛客1号 1000 2 算法 2020-01-01 10:00:00
2 1002 牛客2号 1200 3 算法 2020-01-01 10:00:00
3 1003 进击的3号 2200 5 算法 2020-01-01 10:00:00
4 1004 牛客4号 2500 6 算法 2020-01-01 10:00:00
5 1005 牛客5号 3000 7 C++ 2020-01-01 10:00:00

试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

id uid exam_id start_time submit_time score
1 1001 9001 2020-01-02 09:01:01 2020-01-02 09:21:59 80
3 1001 9002 2021-02-02 19:01:01 2021-02-02 19:30:01 87
2 1001 9001 2021-05-02 10:01:01 (NULL) (NULL)
4 1001 9001 2021-06-02 19:01:01 2021-06-02 19:32:00 20
6 1001 9002 2021-09-01 12:01:01 (NULL) (NULL)
5 1001 9002 2021-09-05 19:01:01 2021-09-05 19:40:01 89
11 1002 9001 2020-01-01 12:01:01 2020-01-01 12:31:01 81
12 1002 9002 2020-02-01 12:01:01 2020-02-01 12:31:01 82
13 1002 9002 2020-02-02 12:11:01 2020-02-02 12:31:01 83
7 1002 9002 2021-05-05 18:01:01 2021-05-05 18:59:02 90
16 1002 9001 2021-09-06 12:01:01 2021-09-06 12:21:01 80
17 1002 9001 2021-09-06 12:01:01 (NULL) (NULL)
18 1002 9001 2021-09-07 12:01:01 (NULL) (NULL)
8 1003 9003 2021-02-06 12:01:01 (NULL) (NULL)
9 1003 9001 2021-09-07 10:01:01 2021-09-07 10:31:01 89
10 1004 9002 2021-08-06 12:01:01 (NULL) (NULL)
14 1005 9001 2021-02-01 11:01:01 2021-02-01 11:31:01 84
15 1006 9001 2021-02-01 11:01:01 2021-02-01 11:31:01 84

题目练习记录表practice_record(uid用户ID, question_id题目ID, submit_time提交时间, score得分):

id uid question_id submit_time score
1 1001 8001 2021-08-02 11:41:01 60
2 1002 8001 2021-09-02 19:30:01 50
3 1002 8001 2021-09-02 19:20:01 70
4 1002 8002 2021-09-02 19:38:01 70
5 1003 8002 2021-09-01 19:38:01 80

请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。

由示例数据结果输出如下:

uid nick_name achievement
1002 牛客2号 1200

解释:昵称以『牛客』开头『号』结尾且成就值在1200~2500之间的有1002、1004;

1002最近一次试卷区活跃为2021年9月,最近一次题目区活跃为2021年9月;1004最近一次试卷区活跃为2021年8月,题目区未活跃。

因此最终满足条件的只有1002。

 思路:
  • 在practice_record,exam_record中筛选出月份是2021年9月
    • where date_format(submit_time, '%Y%m') = '202109' 知识点:date_format
  • 筛选出成就值在1200到2500之间,uid在上述两个任意一个中,且nick_name能匹配牛客_号。where nick_name like '牛客_号' 知识点:like、
select uid,nick_name,achievement	
from user_info
where nick_name like '牛客_号'
and achievement between 1200 and 2500
and uid in (select uid
    from practice_record
    where date_format(submit_time, '%Y%m')='202109'
union all
     select uid
        from exam_record
        where date_format(submit_time, '%Y%m') = '202109')

猜你喜欢

转载自blog.csdn.net/qq_62799214/article/details/127846657
PTA