SQL习题

134 阅读4分钟

一些注意点

  1. group by后面只能+having

1.查询所有列

现在运营想要查看用户信息表中所有的数据,请你取出相应结果

select * from user_profile

嘻嘻

2.查询多列

现在运营同学想要用户的设备id对应的性别、年龄和学校的数据

select device_id,gender,age,university from user_profile

3.查询结果去重

现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。

select distinct university from user_profile

4.查询结果限制返回行数

现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。

select device_id from user_profile limit 0,2

limit后面不用+括号

5.将查询后的列重新命名

现在你需要查看前2个用户明细设备ID数据,并将列名改为 'user_infos_example',,请你从用户信息表取出相应结果。

select device_id as user_infos_example from user_profile limit 0,2

使用as来重新命名列名

6.查找学校是北大的学生信息

现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。

select device_id,university from user_profile where university = '北京大学'

使用where来限定条件

7.查找年龄大于24岁的用户信息

现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。

select device_id,gender,age,university from user_profile where age > 24

8.查找某个年龄段的用户信息

现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。

select device_id,gender,age from user_profile where age >= 20 and age <= 23
select device_id,gender,age from user_profile where age between 20 and 23

2种都行,使用between后不需要加括号

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

现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据

select device_id,gender,age,university from user_profile where university != "复旦大学"

10.用where过滤空值练习

现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。

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

11.AND

现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。

select device_id,gender,age,university,gpa from user_profile where gender = "male" and gpa > 3.5

12.OR

现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现)

select device_id,gender,age,university,gpa from user_profile where university = "北京大学" or gpa > 3.7

13.Where in 和Not in

现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。

select device_id,gender,age,university,gpa from user_profile where university in ("北京大学","复旦大学","山东大学")

14.and和or一起使用

现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据

select device_id,gender,age,university,gpa from user_profile where (university = "复旦大学" and gpa > 3.8) or (university = "山东大学" and gpa > 3.5)

15.查看学校名称中含北京的用户(使用%和LIKE)

现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。

select device_id,age,university from user_profile where university LIKE "北京%"

16.查找GPA最高值(max函数的使用)

运营想要知道复旦大学学生gpa最高值是多少

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

17.计算男生人数以及平均GPA

现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动

select count(*) as male_num,avg(gpa) as avg_gpa from user_profile where gender = 'male'

18.group By

现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

select gender,university,count(*) 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

19.group by 与having

现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。

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

20group by 与order by

现在运营想要查看不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列,请你取出相应数据。

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

21.子查询

现在运营想要查看所有来自浙江大学的用户题目回答明细情况,请你取出相应数据

1.子查詢

select device_id,question_id,result from question_practice_detail q where (select university from user_profile u where u.device_id = q.device_id) = '浙江大学'

2.子查询2

select device_id,question_id,result from question_practice_detail q where device_id = (select device_id from user_profile u where u.device_id = q.device_id and university = "浙江大学")

22.连接join

运营想要了解每个学校答过题的用户平均答题数量情况

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