文章目录
- 1. SQL21 浙江大学用户题目回答情况
- ① 错误
- ② 正确
- 2. SQL22 统计每个学校的答过题的用户的平均答题数
- ① 错误
- ② 正确
- 3. SQL23 统计每个学校各难度的用户平均刷题数
- 4. SQL25 查找山东大学或者性别为男生的信息
- ① 错误
- ② 正确
- 5. SQL26 计算25岁以上和以下的用户数量
- ① 错误
- ② 正确
- 6. SQL29 计算用户的平均次日留存率
- 7. SQL33 找出每个学校GPA最低的同学
- ① 错误
- ② 正确
- 8. SQL34 统计复旦用户8月练题情况
- ① 错误
- ② 正确
- 9. SQL35 浙大不同难度题目的正确率
- ① 错误
- ② 正确
1. SQL21 浙江大学用户题目回答情况
SQL21 浙江大学用户题目回答情况
① 错误
最开始想的是用内连接
select question_practice_detail.device_id, question_id, result from user_profile right join question_practice_detail on user_profile.device_id = question_practice_detail.device_id;
② 正确
看了一下讨论的部分发现我之前错的原因在于没有限定学校为浙江大学
内连接:
select question_practice_detail.device_id, question_id, result from user_profile inner join question_practice_detail on user_profile.device_id = question_practice_detail.device_id where university = '浙江大学';
右连接:
select question_practice_detail.device_id, question_id, result from user_profile right join question_practice_detail on user_profile.device_id = question_practice_detail.device_id where university = '浙江大学';
还看到一个用子查询解决的,试着写了一下,发现可以
select device_id, question_id, result from question_practice_detail where question_practice_detail.device_id in (select device_id from user_profile where university = '浙江大学' );
眼瞎了一下,又看了一下题目列表,发现这题的标签写的就是子查询🤣
2. SQL22 统计每个学校的答过题的用户的平均答题数
SQL22 统计每个学校的答过题的用户的平均答题数
① 错误
select university, count(question_id) / count(question_practice_detail.device_id) as avg_answer_cnt from user_profile right join question_practice_detail on user_profile.device_id = question_practice_detail.device_id group by university order by avg_answer_cnt;
我感觉问题主要在于如何给 question_practice_detail 中的device_id去重
② 正确
最后还是看了题解。之前把去重写成了 count(question_id) / distinct count(question_practice_detail.device_id),结果不对。
select university, count(question_id) / count(distinct(question_practice_detail.device_id)) as avg_answer_cnt from user_profile inner join question_practice_detail on user_profile.device_id = question_practice_detail.device_id group by university;
order by avg_answer_cnt也不能写。因为这会导致出现两次排序
3. SQL23 统计每个学校各难度的用户平均刷题数
SQL23 统计每个学校各难度的用户平均刷题数
select university, difficult_level, count(question_practice_detail.question_id) / count(distinct(question_practice_detail.device_id)) from user_profile inner join question_practice_detail inner join question_detail on user_profile.device_id = question_practice_detail.device_id and question_practice_detail.question_id = question_detail.question_id group by university , difficult_level;
注意最后group by university , difficult_level; ,如果两个分类依据用and连接会报错
4. SQL25 查找山东大学或者性别为男生的信息
SQL25 查找山东大学或者性别为男生的信息
① 错误
没有限定输出顺序
select all device_id, gender, age, gpa from user_profile where university = '山东大学' or gender = 'male';
② 正确
不太知道怎么确认输出顺序,所以看了一下题解。解决方案是用union关联两个查询语句,并且先查山东大学的
select device_id, gender, age, gpa from user_profile where university = '山东大学' union all select device_id, gender, age, gpa from user_profile where gender = 'male';
5. SQL26 计算25岁以上和以下的用户数量
SQL26 计算25岁以上和以下的用户数量
① 错误
select age_cut , if(age >= 25, '25岁及以上', '25岁以下') as age from user_profile group by age;
② 正确
不太知道怎么分别显示出这俩分类的字段,也不太知道该怎么分别统计,所以看了一下题解
select if(age >= 25, '25岁及以上', '25岁以下') as age_cut, count(*) as result from user_profile group by age_cut;
select ( case when age >= 25 then '25岁及以上' else '25岁以下' end ) as age_cut, count(*) as result from user_profile group by age_cut;
select ( case when age < 25 or age is null then '25岁以下' when age >= 25 then '25岁及以上' end )as age_cut, count(*) as result from user_profile group by age_cut;
6. SQL29 计算用户的平均次日留存率
SQL29 计算用户的平均次日留存率
样例的结果都不知道怎么来的,所以就看了题解 题解地址
date_add()函数
select count(distinct nq.device_id, datee) / count(distinct q.device_id, date) from (select distinct device_id, date from question_practice_detail) as q left join (select distinct device_id, date_add(date, interval 1 day) as datee from question_practice_detail) as nq on q.device_id = nq.device_id and q.date = nq.datee;
7. SQL33 找出每个学校GPA最低的同学
SQL33 找出每个学校GPA最低的同学
窗口函数
① 错误
看了窗口函数这个教程后写的,现在的问题在于没有去重。这条语句的结果是查出来所有学生的信息,然后该生所属学校的最低gpa表示其gpa
select distinct device_id, university, min(gpa) over (partition by university)from user_profile;
② 正确
方法一:子查询
【中字】SQL进阶教程 | 史上最易懂SQL教程
方法二:窗口函数
select device_id, university, gpa from ( select device_id, university, gpa, row_number() over (partition by university order by gpa) as rk from user_profile )as a where rk = 1;
8. SQL34 统计复旦用户8月练题情况
SQL34 统计复旦用户8月练题情况
① 错误
不知道该怎么统计题目数量,以及正确的题目数据。下面这个只是筛选出了符合条件的sql语句
select user_profile.device_id, university, result from user_profile left join question_practice_detail on user_profile.device_id = question_practice_detail.device_id having university = '复旦大学';
select count(result) from question_practice_detail where result = 'right';
select count(result) from question_practice_detail where result = 'right';
代码异常:
select user_profile.device_id, university, count(q)as right_question_cnt from user_profile where device_id in (select device_id from question_practice_detail where result = 'right') as q on user_profile.device_id = question_practice_detail.device_id having university = '复旦大学';
② 正确
select user_profile.device_id, university, sum(if(result is null, 0, 1))as question_cnt, sum(if(result = 'right', 1, 0)) as right_question_cnt from user_profile left join question_practice_detail on user_profile.device_id = question_practice_detail.device_id where university = '复旦大学' and (month(date) = 8 or date is null) group by user_profile.device_id;
9. SQL35 浙大不同难度题目的正确率
SQL35 浙大不同难度题目的正确率
① 错误
select difficult_level, (sum(if(result = 'right', 1, 0)) / sum(if(result is null, 0, 1))) as correct_rate from user_profile left join question_practice_detail left join question_detail on user_profile.device_id = question_practice_detail.device_id and question_practice_detail.question_id = qeustion_detail.question_id where university = '浙江大学' group by difficult_level;
① 除去代码异常外,忽略的条件:按照正确率升序排序
② 代码错误原因:主表选择错误、from 后面没有加括号、两个连接条件应该分开写
代码异常:
select ( case when result = 'easy' then 'easy' when result = 'medium' then 'medium' when result = 'hard' then 'hard' end ) as difficult_level, count(*) as test from user_profile left join question_practice_detail left join question_detail on user_profile.device_id = question_practice_detail.device_id and question_practice_detail.question_id = qeustion_detail.question_id where university = '浙江大学'group by difficult_level;
② 正确
题解
看了题解后写的,有略微改动:
select difficult_level, (sum(if(result = 'right', 1, 0)) / sum(if(result is null, 0, 1))) as correct_rate from (question_practice_detail left join user_profile on question_practice_detail.device_id = user_profile.device_id left join question_detail on question_detail.question_id = question_practice_detail.question_id )where university = '浙江大学' group by difficult_level order by correct_rate asc;