本文主要是介绍【SQL】牛客网SQL非技术入门40道代码|练习记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
跟着刷题:是橘长不是局长哦_哔哩哔哩_bilibili
6查询学校是北大的学生信息
select
device_id, university
from user_profile
where university = '北京大学'
7查找年龄大于24岁的用户信息
select
device_id, gender, age, university
from user_profile
where age > 24
8查找某个年龄段的用户信息
select
device_id, gender, age
from user_profile
where age >= 20 and age <=23
9查找去除复旦大学的用户信息
select
device_id, gender, age, university
from user_profile
where university != '复旦大学'
10用where过滤空值练习
select
device_id, gender, age, university
from user_profile
where age is not null
11高级操作符练习(1)
select
device_id, gender, age, university, gpa
from user_profile
where gender = 'male' and gpa >3.5
12高级操作符练习(2)
select
device_id, gender, age, university, gpa
from user_profile
where university = '北京大学' or gpa >3.7
13Where in 和Not in
select
device_id, gender, age, university, gpa
from user_profile
where university in ('北京大学', '复旦大学', '山东大学')
14操作符混合运用
select
device_id, gender, age, university, gpa
from user_profile
where gpa>3.5 and university='山东大学'
or gpa>3.8 and university='复旦大学'
15查看学校名称中含北京的用户
selectdevice_id,age,universityfromuser_profile
whereuniversity like '%北京%'
16查找GPA最高值
执行顺序是先where再from
excel里也是先筛选再聚合??为什么捏?好处是:先筛选之后数据量就变少了,然后再计算(或者更复杂的操作),数据量越少,执行速度就快,大概率
select
round(max(gpa), 1)
from user_profile
where university = '复旦大学'
17计算男生人数以及他们的平均GPA
函数count()
可以是
- count(id):统计某个列中非 NULL 值的数量
- count(*):统计表中的总行数
- count(1):这与
COUNT(*)
相同
select
round(count(1), 1)
,round(avg(gpa), 1)
from user_profile
where gender='male'
17改题:计算男生人数以及全班的平均GPA
count(if())
select
round(count(if gender = 'male', 1, null), 1)
,round(avg(gpa), 1)
from user_profile
18分组计算练习题
分组聚合
selectgender,university,round(count(device_id), 1),round(avg(active_days_within_30), 1),round(avg(question_cnt), 1)
fromuser_profile
group by1,2
19分组过滤练习题
在excel里会怎么做:先求出平均发帖和回帖情况(用数据透视表做),然后再筛选符合条件的
关键字:having,having是在聚合之后的数据里进行筛选
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
解法2:可以用子查询
不过having存在,就是让你可以少写一层子查询,仅此
select
*
from(selectuniversity,avg(question_cnt) as avg_question_cnt,avg(answer_cnt) as avg_answer_cntfromuser_profilegroup byuniversity
) as a
where avg(question_cnt) < 5 or avg(answer_cnt) < 20
20分组排序练习
关键字:order by,默认升序排列;降序加desc,如 order by 2 desc
select
university
,avg(question_cnt)
from user_profile
group by 1
order by 2
21浙江大学用户题目回答情况
excel里XLOOKUP,只连接某个字段
关键字join,表连接
- 左连接:left join,保证左边的表不变,右表拼接上来,如果没有拼上,右边的那条数据就不要了,保证了左表的完整性
- 右连接:
- 全连接:为空也保留,左右都保留
- 内连接:join,只要一边没连上全都丢掉
1)只连接指定列:
select from question_practice_detail a
left join
(selectdevice_id, universityfrom user_profile
) b
on a.device_id = b.device_id
2)连接所有列:
select from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
先连接,后筛选,
select
a.device_id as device_id
,a.question_id as question_id
,a.result as result
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
where b.university = '浙江大学'
22统计每个学校的答过题的用户的平均题数
说明:某学校用户平均答题数量计算方式为该学校用户答题总次数除以答过题的不同用户个数
select
b.university as university
, count(1) / count(distinct a.device_id) as avg_answer_cnt
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
group by 1
order by 1
23
事实表、维度表、信息表?
第一步连接(多表连接)
selectfrom question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
left join question_detail c
on a.question_id = c.question_id
bug?刚刚在牛客提交,明明是一样的答案提交错误,等了一会就可以了
select
b.university as university
,c.difficult_level as difficult_level
,count(1) / count(distinct a.device_id) as avg_answer_cnt
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
left join question_detail c
on a.question_id = c.question_id
group by 1, 2
24
这篇关于【SQL】牛客网SQL非技术入门40道代码|练习记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!