select device_id, gender, age, university from user_profile
简单处理查询结果
SQL3 查询结果去重
参考代码
selectdistinct(university)from user_profile
SQL4 查询结果限制返回行数
参考代码
select device_id from user_profile limit2
SQL5 将查询后的列重新命名
参考代码
SELECT device_id user_infos_example FROM user_profile LIMIT2
02 条件查询
基础操作符
SQL6 查找学校是北大的学生信息
参考代码
SELECT device_id, university FROM user_profile WHERE university ='北京大学'
SQL7 查找年龄大于24岁的用户信息
参考代码
SELECT device_id, gender, age, university FROM user_profile WHERE age >=24
SQL8 查找某个年龄段的用户信息
参考代码
SELECT device_id, gender, age FROM user_profile
WHERE age BETWEEN20AND23
SQL9 查找除复旦大学的用户信息
参考代码
SELECT device_id, gender, age, university FROM user_profile
WHERE university <>'复旦大学'
SQL10 用where过滤空值练习
参考代码
SELECT device_id, gender, age, university FROM user_profile
WHERE age ISNOTNULL
高级操作符
SQL11 高级操作符练习(1)
参考代码
SELECT device_id, gender, age, university, gpa FROM user_profile
WHERE gender ='male'AND gpa >3.5
SQL12 高级操作符练习(2)
参考代码
SELECT device_id, gender, age, university, gpa FROM user_profile
WHERE university ='北京大学'OR gpa >3.7
SQL13 Where in 和Not in
参考代码
SELECT device_id, gender, age, university, gpa FROM user_profile
WHERE university IN('北京大学','复旦大学','山东大学')
SQL14 操作符混合运用
参考代码
SELECT device_id, gender, age, university, gpa
FROM user_profile
where(gpa >3.5and university ='山东大学')OR(gpa >3.8and university ='复旦大学')
SQL15 查看学校名称中含北京的用户
参考代码
SELECT device_id, age, university FROM user_profile
WHERE university LIKE'%北京%'
03 高级查询
计算函数
SQL16 查找GPA最高值
参考代码
SELECT gpa FROM user_profile
WHERE university ='复旦大学'ORDERBY gpa DESCLIMIT1
SQL17 计算男生人数以及平均GPA
参考代码
SELECTCOUNT(gender)AS male_num,ROUND(AVG(gpa),1)AS avg_gpa
FROM user_profile
WHERE gender ='male'
分组查询
SQL18 分组计算练习题
参考代码
SELECT gender,
university,COUNT(device_id)AS user_num,ROUND(AVG(active_days_within_30),1)AS avg_active_day,ROUND(AVG(question_cnt),1)AS avg_question_cnt
FROM user_profile
GROUPBY gender, university
SQL18 分组计算练习题
参考代码
SELECT university,ROUND(AVG(question_cnt),3)AS avg_question_cnt,ROUND(AVG(answer_cnt),3)AS avg_answer_cnt
FROM user_profile
GROUPBY university
HAVING avg_question_cnt <5OR avg_answer_cnt <20
SQL20 分组排序练习题
参考代码
SELECT university,ROUND(AVG(question_cnt),4)AS avg_question_cnt
FROM user_profile
GROUPBY university
ORDERBY avg_question_cnt
04 多表查询
子查询
SQL21 浙江大学用户题目回答情况
参考代码
SELECT A.device_id,
A.question_id,
A.result
FROM question_practice_detail A
LEFTJOIN user_profile B
ON A.device_id = B.device_id
WHERE B.university ='浙江大学'ORDERBY A.question_id
连接查询
SQL22 统计每个学校的答过题的用户的平均答题数
参考代码
SELECT A.university,COUNT(B.question_id)/COUNT(DISTINCT(A.device_id))AS avg_answer_cnt
FROM user_profile AS A
JOIN question_practice_detail AS B
ON A.device_id = B.device_id
GROUPBY A.university
SQL23 统计每个学校各难度的用户平均刷题数
参考代码
SELECT A.university,
C.difficult_level,COUNT(B.question_id)/COUNT(DISTINCT(A.device_id))AS avg_answer_cnt
FROM user_profile AS A
JOIN question_practice_detail AS B
ON A.device_id = B.device_id
JOIN question_detail AS C
ON B.question_id = C.question_id
GROUPBY A.university, C.difficult_level
SQL24 统计每个用户的平均刷题数
参考代码
SELECT T1.university,
T3.difficult_level,ROUND(COUNT(T2.question_id)/COUNT(DISTINCT(T1.device_id)),4)AS avg_answer_cnt
FROM user_profile AS T1
JOIN question_practice_detail T2
ON T1.device_id = T2.device_id
JOIN question_detail T3
ON T2.question_id = T3.question_id
WHERE T1.university ='山东大学'GROUPBY T1.university, T3.difficult_level
组合查询
SQL25 查找山东大学或者性别为男生的信息
参考代码
SELECT device_id,
gender,
age,
gpa
FROM user_profile
WHERE university ='山东大学'UNIONALLSELECT device_id,
gender,
age,
gpa
FROM user_profile
WHERE gender ='male'
05 必会的常用函数
条件函数
SQL26 计算25岁以上和以下的用户数量
参考代码
SELECTCASEWHEN age <25OR age ISNULLTHEN'25岁以下'WHEN age >=25THEN'25岁及以上'END age_cut,COUNT(*)number
FROM user_profile
GROUPBY age_cut
SQL27 查看不同年龄段的用户明细
参考代码
SELECT device_id,
gender,CASEWHEN age <20THEN'20岁以下'WHEN age BETWEEN20AND24THEN'20-24岁'WHEN age >=25THEN'25岁及以上'ELSE'其他'END age_cut
FROM user_profile
日期函数
SQL28 计算用户8月每天的练题数量
参考代码
SELECTDAY(date)ASday,COUNT(question_id)AS question_cnt
FROM question_practice_detail
WHERELEFT(date,7)='2021-08'GROUPBYday
SQL29 计算用户的平均次日留存率
参考代码
SELECTCOUNT(T2.date)/COUNT(T1.date)FROM(SELECTDISTINCT device_id,dateFROM question_practice_detail) T1
LEFTJOIN(SELECTDISTINCT device_id,dateFROM question_practice_detail) T2
ON T1.device_id = T2.device_id
AND T2.date= date_add(T1.date,interval1day)
文本函数
SQL30 统计每种性别的人数
参考代码
SELECT SUBSTRING_INDEX(profile,',',-1)AS gender,COUNT(device_id)AS number
FROM user_submit
GROUPBY gender
SQL31 提取博客URL中的用户名
参考代码
SELECT device_id,
SUBSTRING_INDEX(blog_url,'/',-1)AS user_name
FROM user_submit
SQL32 截取出年龄
参考代码
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(profile,',',-2),',',1)AS age,COUNT(device_id)AS number
FROM user_submit
GROUPBY age
窗口函数
SQL33 找出每个学校GPA最低的同学
参考代码
SELECT device_id,
university,
gpa
FROM(SELECT device_id,
university,
gpa,
ROW_NUMBER()OVER(PARTITIONBY university ORDERBY gpa)AS RK
FROM user_profile
)T
WHERE T.RK =1
06 综合练习
综合练习
SQL34 统计复旦用户8月练题情况
参考代码
SELECT T1.device_id,
T1.university,SUM(IF(T2.result isNOTNULL,1,0))AS question_cnt,SUM(IF(T2.result ='right',1,0))AS right_question_cnt
FROM user_profile AS T1
LEFTJOIN question_practice_detail AS T2
ON T1.device_id=T2.device_id
WHERE T1.university='复旦大学'AND(MONTH(T2.date)=8OR T2.dateisNULL)GROUPBY T1.device_id
SQL35 浙大不同难度题目的正确率
参考代码
SELECT
T3.difficult_level,ROUND(SUM(IF(T2.result ='right',1,0))/COUNT(T2.result),4)AS correct_rate
FROM user_profile AS T1
LEFTJOIN question_practice_detail AS T2
ON T1.device_id = T2.device_id
LEFTJOIN question_detail T3
ON T2.question_id = T3.question_id
WHERE T1.university ='浙江大学'AND T3.difficult_level ISNOTNULLGROUPBY T3.difficult_level
ORDERBY correct_rate
SQL36 查找后排序
参考代码
SELECT device_id,
age
FROM user_profile
ORDERBY age
SQL37 查找后多列排序
参考代码
SELECT device_id,
gpa,
age
FROM user_profile
ORDERBY gpa, age
SQL38 查找后降序排列
参考代码
SELECT device_id,
gpa,
age
FROM user_profile
ORDERBY gpa DESC, age DESC
SQL39 21年8月份练题总数
参考代码
SELECTCOUNT(DISTINCT(device_id))AS did_cnt,COUNT(question_id)AS question_cnt
FROM question_practice_detail
WHERELEFT(date,7)='2021-08'ORDERBY did_cnt
参考代码优化,面对大数据时提升效率
SELECTCOUNT(did_cnt),SUM(question_cnt)FROM(SELECTCOUNT(device_id)AS did_cnt,COUNT(question_id)AS question_cnt
FROM question_practice_detail
WHEREdate>='2021-08-01'anddate<='2021-08-31'GROUPBY device_id
)T