牛客SQL练习篇题解

牛客SQL练习篇题解

  • 题解

题解

SQL1 查询所有列

select * from user_profile

SQL2 查询多列

select device_id,gender,age,university from user_profile

SQL3 查询结果去重

select distinct university from user_profile

SQL4 查询结果限制返回行数

select device_id from user_profile limit 0,2

SQL5 将查询后的列重新命名

select device_id as user_infos_example from user_profile limit 0,2

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 between 20 and 23

SQL9 查找除复旦大学的用户信息

select device_id,gender,age,university from user_profile where university not in ('复旦大学')

SQL10 用where过滤空值练习

select device_id,gender,age,university from user_profile where age is not null

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.5 and university='山东大学' or gpa>3.8 and university='复旦大学'

SQL15 查看学校名称中含北京的用户

select device_id,age,university from user_profile where university like '%北京%'

SQL16 查找GPA最高值

select max(gpa) from user_profile where university='复旦大学'

SQL17 计算男生人数以及平均GPA

select count(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,
    avg(active_days_within_30) as avg_active_day,
    avg(question_cnt) as avg_question_cnt
from
    user_profile
group by
    gender,
    university

SQL19 分组过滤练习题

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

SQL20 分组排序练习题

select university,avg(question_cnt) as avg_question_cnt from user_profile  group by university order by avg_question_cnt asc

SQL21 浙江大学用户题目回答情况

select device_id,question_id,result from question_practice_detail where device_id in(select device_id from user_profile where university='浙江大学') order by question_id asc

SQL22 统计每个学校的答过题的用户的平均答题数

select university,count(question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from user_profile as up inner join question_practice_detail as qpd on up.device_id=qpd.device_id
group by university

SQL23 统计每个学校各难度的用户平均刷题数

select
    university,
    difficult_level,
    round(count(qpd.question_id) / count(distinct qpd.device_id),4) as avg_answer_cnt
from
    question_practice_detail qpd,
    user_profile up,
    question_detail qd
where
    qpd.device_id = up.device_id and qpd.question_id = qd.question_id
group by
    university,difficult_level

SQL24 统计每个用户的平均刷题数

select university,difficult_level,(count(qpd.question_id)) / (count(distinct qpd.device_id)) as avg_answer_cnt
from question_practice_detail qpd,
user_profile up,
question_detail qd
where qpd.device_id=up.device_id and qpd.question_id=qd.question_id and up.university="山东大学"
group by difficult_level

SQL25 查找山东大学或者性别为男生的信息

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'

SQL26 计算25岁以上和以下的用户数量

select if(age>=25,"25岁及以上","25岁以下") as age_cut,count(*) as number from user_profile group by age_cut

SQL27 查看不同年龄段的用户明细

select device_id,gender,case when age>=25 then '25岁及以上' when age>=20 then '20-24岁' when age<20 then '20岁以下' else '其他' end as age_cnt from user_profile

SQL28 计算用户8月每天的练题数量

select day(date) as day,count(question_id) as question_cnt
from question_practice_detail
where date like '2021-08-%'
group by day

SQL29 计算用户的平均次日留存率

select count(q2.device_id) / count(q1.device_id) as avg_ret
from (select distinct device_id,date from question_practice_detail) as q1
left join
(select distinct device_id,date from question_practice_detail) as q2
on q1.device_id = q2.device_id and q2.date=date_add(q1.date, interval 1 day)

SQL30 统计每种性别的人数

select substring_index(profile,",",-1) as gender,count(*) as number
from user_submit
group by gender

SQL31 提取博客URL中的用户名

select device_id,substring_index(blog_url,"/",-1) as user_name from user_submit

SQL32 截取出年龄

select
    substring_index(substring_index(profile, ',', 3), ',', -1) as age,
    count(device_id) as number
from user_submit
group by age

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

select device_id,university,gpa
from user_profile u
where gpa=
(select min(gpa) from user_profile where university=u.university)
order by university

select device_id,university,gpa from
(select device_id,university,gpa,row_number() over (partition by university order by gpa asc) rk from user_profile) a
where a.rk=1

SQL34 统计复旦用户8月练题情况

select up.device_id,'复旦大学' as university,count(question_id) as question_cnt,sum(if(qpd.result='right',1,0)) as right_question_cnt
from user_profile as up
left join question_practice_detail as qpd
on qpd.device_id = up.device_id and month(qpd.date) = 8
where up.university='复旦大学'
group by up.device_id

SQL35 浙大不同难度题目的正确率

select difficult_level,avg(if(qpd.result='right',1,0)) as correct_rate
from user_profile as up
inner join question_practice_detail as qpd 
on up.device_id = qpd.device_id
inner join question_detail as qd
on qd.question_id = qpd.question_id
where up.university = '浙江大学'
group by qd.difficult_level
order by correct_rate asc;

SQL36 查找后排序

select device_id,age
from user_profile
order by age asc

SQL37 查找后多列排序

select device_id,gpa,age
from user_profile
order by gpa asc,age asc

SQL38 查找后降序排列

select device_id,gpa,age
from user_profile
order by gpa desc,age desc

SQL39 21年8月份练题总数

select count(distinct device_id) as did_cnt,count(question_id) as question_cnt
from question_practice_detail
where date like '%2021-08%'

❤️希望对您有帮助,您的支持是我创作最大的动力!

你可能感兴趣的:(SQL,sql,数据库)