做網(wǎng)站雙12促銷方案百度官方免費下載安裝
題目1——查找GPA最高值
想要知道復旦大學學生gpa最高值是多少,請你取出相應數(shù)據(jù)
題解:
1、使用MAX
select MAX(gpa) FROM user_profile?WHERE university = '復旦大學'
2、使用降序排序組合limit
select gpa FROM user_profile WHERE university = '復旦大學' order by gpa desc limit 1
題目2——計算男生人數(shù)以及平均GPA
想要看一下男性用戶有多少人以及他們的平均gpa是多少,用以輔助設計相關活動,請你取出相應數(shù)據(jù)
題解:
按照要求進行條件分解:
限定條件為 男性用戶;
有多少人,明顯是計數(shù),count函數(shù);
平均gpa,求平均值用avg函數(shù);
細節(jié)問題:根據(jù)輸出示例,有兩個問題需要注意:
表頭重命名,用as語法
浮點數(shù)的平均值可能小數(shù)點位數(shù)很多,按照示例保存一位小數(shù),用round函數(shù)
select ?count(gender) as male_num,round(avg(gpa),1) as avg_gpa
from user_profile
where gender="male"
題目3——分組計算練習題
想要對每個學校不同性別的用戶活躍情況和發(fā)帖數(shù)量進行分析,請分別計算出每個學校每種性別的用戶數(shù)、30天內平均活躍天數(shù)和平均發(fā)帖數(shù)量
題解:先進行條件分解
限定條件:無;
每個學校每種性別:按學校和性別分組:group by gender, university
用戶數(shù):count(device_id)
30天內平均活躍天數(shù):avg(active_days_within_30)
平均發(fā)帖數(shù)量:avg(question_cnt)
select
gender,university,
count(device_id) as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university
題目4——分組過濾練習題
想查看每個學校用戶的平均發(fā)貼和回帖情況,尋找低活躍度學校進行重點運營,請取出平均發(fā)貼數(shù)低于5的學校或平均回帖數(shù)小于20的學校。
題解:
having篩選,having常用于對分組結果進行篩選。
這里不能使用where的原因是 where的執(zhí)行順序早于group by,所以where 也不可以使用 列的別名。而having可以,執(zhí)行順序如下:
答案:
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
題目5——分組排序練習題
想要查看不同大學的用戶平均發(fā)帖情況,并期望結果按照平均發(fā)帖情況進行升序排列,請你取出相應數(shù)據(jù)。
題解:
select university,avg(question_cnt) as avg_question_cnt
from user_profile
group by university
order by avg_question_cnt