MySQL牛客网习题
查询结果去重
1 | select distinct university from user_profile; |
查询结果并限制返回行数
1 | select device_id from user_profile limit 2; |
将查询后的列重新命名
1 | select device_id as user_infos_example from user_profile limit 2; |
查找学校是北大的学生信息
1 | select device_id, university from user_profile where university = '北京大学'; |
查找年龄大于24岁的用户信息
1 | select device_id, gender, age, university from user_profile where age > '24'; |
查找某个年龄段的用户信息
1 | select device_id, gender, age from user_profile where age >= 20 and age <= 23; |
查找除复旦大学的用户信息
1 | select device_id, gender, age, university from user_profile |
用where过滤空值练习
1 | select device_id, gender, age, university from user_profile |
高级操作符练习
1 | select device_id, gender, age, university, gpa from user_profile where gender = 'male' and gpa > '3.5'; |
Where in 和Not in
1 | select device_id, gender, age, university, gpa from user_profile where university in ('北京大学', '复旦大学', '山东大学'); |
查看学校名称中含北京的用户
1 | select device_id, age, university from user_profile where university like '%北京%'; |
查找GPA最高值
1 | select gpa from user_profile where university = '复旦大学' order by gpa desc limit 1; |
计算男生人数以及平均GPA
1 | select count(gender) as male_num, round(avg(gpa), 1) as avg_gpa from user_profile where gender = 'male'; |
分组计算练习题
1 | select gender, university, count(gender) as user_num, avg(active_days_within_30) as avg_active_day, avg(question_cnt) as avg_question_cnt from user_profile group by university, gender; |
分组过滤练习题
1 | select university, avg(question_cnt) as avg_question_cnt, avg(answer_cnt) as avg_answer_cnt from user_profile group by university having avg_question_cnt < 5 or avg_answer_cnt < 20; |
分组排序练习题
1 | select university, avg(question_cnt) as avg_question_cnt from user_profile group by university order by avg_question_cnt asc; |
浙江大学用户题目回答情况
1 | # 连接查询 |
统计每个学校的答过题的用户的平均答题数
1 | select university, count(q.question_id) / count(distinct(q.device_id)) as avg_answer_cnt from question_practice_detail q, user_profile u where q.device_id = u.device_id group by university; |
统计每个学校各难度的用户平均刷题数
1 | select u.university, qd.difficult_level as difficult_level,count(q.question_id) / count(distinct(q.device_id)) as avg_answer_cnt from user_profile u, question_practice_detail q, question_detail qd where q.device_id = u.device_id and q.question_id = qd.question_id group by u.university, qd.difficult_level; |
统计每个用户的平均刷题数
1 | select u.university, qd.difficult_level, count(q.question_id) / count(distinct(q.device_id)) as avg_answer_cnt from |
查找山东大学或者性别为男生的信息
1 | 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' |
计算25岁以上和以下的用户数量
1 | select if(age >= 25, '25岁及以上', '25岁以下') as age_cut, count(device_id) as number |
查看不同年龄段的用户明细
1 | #方法一: union |
计算用户8月每天的练题数量
1 | select day(date) as day, count(question_id) as question_cnt |
计算用户的平均次日留存率
1 | select count(distinct q2.device_id, q2.date) / count(distinct q1.device_id, q1.date) as avg_ret |
对于SQL语句中where和on的理解_不爱吃垃圾的小王的博客-CSDN博客_sql中的on的用法
统计每种性别的人数
1 | select substring_index(profile, ',', '-1') as gender, count(device_id) as number from user_submit group by gender; |
提取博客URL中的用户名
1 | select device_id, substring_index(blog_url, '/', '-1') as user_name from user_submit; |
截取出年龄
1 | select substring_index(substring_index(profile, ',', '-2'), ',', '1') as age, count(device_id) as number from user_submit group by age; |
找出每个学校GPA最低的同学
1 | select device_id, university, gpa from user_profile u |
统计复旦用户8月练题情况
1 | select u.device_id, university, sum(if(result is not null, 1, 0))as question_cnt, |
浙大不同难度题目的正确率
1 | select difficult_level, avg(IF(result = 'right',1, 0)) as correct_rate |
查找后排序
1 | select device_id, age from user_profile order by age asc; |
查找后多列排序
1 | select device_id, gpa, age from user_profile order by gpa, age; |
查找后降序排列
1 | select device_id, gpa, age from user_profile order by gpa desc, age desc; |
21年8月份练题总数
1 | select count(distinct(device_id)) as did_cnt, count(question_id) as question_cnt from question_practice_detail where substring(date,1,7) = '2021-08'; |