如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。
为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1。
检索记录行1-2
select device_id from user_profile
limit 0,2
SQL5 将查询后的列重新命名
- 题目:现在你需要查看前2个用户明细设备ID数据,并将列名改为 'user_infos_example',,请你从用户信息表取出相应结果。
select device_id as user_infos_example
from user_profile
limit 2
as 别名可省略
SQL6 查找学校是北大的学生信息
-
题目:现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。
-
Select device_id ,university from user_profile
where university="北京大学"
SQL7 查找年龄大于24岁的用户信息
-
题目:现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。
-
select device_id,gender,age,university from user_profile
where age>24
SQL8 查找某个年龄段的用户信息
- 题目:现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。
select device_id,gender,age from user_profile
where age BETWEEN 20 and 23
#where age >=20 and age <=23
between and 是闭区间注意
SQL9 查找除复旦大学的用户信息
题目:现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据
select device_id,gender,age,university from user_profile
#where university <> "复旦大学"
#where university != "复旦大学"
#where university not like "复旦大学"
where university not in ( "复旦大学")
SQL10 用where过滤空值练习
-
题目:现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。
-
select device_id ,gender,age,university from user_profile
#where age not like" null"
#where age not in (" null")
#where age!=" null"
where age<>" null"
SQL11 高级操作符练习(1)
-
题目:现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。
-
select device_id,gender,age,university,gpa from user_profile
where gender="male"and gpa>"3.5"
SQL12 高级操作符练习(2)
-
题目:现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据
-
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 ("北京大学","复旦大学","山东大学")
where university not IN ("浙江大学")
SQL14 操作符混合运用
-
题目:现在运营想要找到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.5 )
or (university="复旦大学" and gpa>3.8);
SQL15 查看学校名称中含北京的用户
- 题目:现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。
select device_id,age,university from user_profile
#where university like "%北京%"
WHERE university REGEXP "北京"
% :百分号 代表匹配0个或多个字符
_:一个字符
SQL16 查找GPA最高值
- 题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据
select max(gpa) from user_profile
where university ="复旦大学"
SQL17 计算男生人数以及平均GPA
-
题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。
-
select count(gender) male_num,round(avg(gpa),1) avg_gpa
from user_profile
where gender='male'
round(a,b)返回a的第几位小数
SQL18 分组计算练习题
-
用户信息表:user_profile
-
30天内活跃天数字段(active_days_within_30)
-
发帖数量字段(question_cnt)
-
回答数量字段(answer_cnt)
select gender,
university,
count(gender) user_num,
avg(active_days_within_30 )avg_active_day,
avg(question_cnt) avg_question_cnt
from user_profile
group by gender ,university
SQL19 分组过滤练习题
-
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于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
SQL20 分组排序练习题
题目:现在运营想要查看不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列,请你取出相应数据。
select university,
avg(question_cnt) as avg_question_cnt
from user_profile
group by university
order by avg(question_cnt)
order by 默认升序排列
SQL21 浙江大学用户题目回答情况
题目:现在运营想要查看所有来自浙江大学的用户题目回答明细情况,请你取出相应数据
方法1:join两个表,用inner join on a.device_id=b.device_id
select a.device_id,a.question_id,a.result
from question_practice_detail a
inner join user_profile b
on a.device_id=b.device_id
where b.university = "浙江大学"
方法2:先从画像表找到浙江大学的所有学生id列表
select device_id, question_id, result
from question_practice_detail
where device_id in (
select device_id from user_profile
where university='浙江大学'
)
SQL22 统计每个学校的答过题的用户的平均答题数
题目:运营想要了解每个学校答过题的用户平均答题数量情况,请你取出数据。
select distinct a.university ,COUNT(b.question_id)/COUNT(distinct(a.device_id)) avg_answer_cnt
from user_profile a inner join
question_practice_detail b
on a.device_id=b.device_id
group by university
- 平均答题数量:在每个学校的分组内,用总答题数量除以总人数即可得到平均答题数量
count(question_id) / count(distinct device_id)
。
SQL23 统计每个学校各难度的用户平均刷题数
题目:运营想要计算一些参加了答题的不同学校、不同难度的用户平均答题量,请你写SQL取出相应数据
select distinct a.university,c.difficult_level,count(b.question_id)/COUNT(distinct(b.device_id))
from user_profile a ,question_practice_detail b,question_detail c
where a.device_id=b.device_id
and b.question_id=c.question_id
group by a.university,c.difficult_level
SQL24 统计每个用户的平均刷题数
题目:运营想要查看参加了答题的山东大学的用户在不同难度下的平均答题题目数,请取出相应数据
select a.university ,c.difficult_level, count(b.question_id)/ count(distinct b.device_id) avg_answer_cnt
from user_profile a,question_practice_detail b,question_detail c
where
a.device_id=b.device_id and
b.question_id=c.question_id
and university="山东大学"
SQL25 查找山东大学或者性别为男生的信息
题目:现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。
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"
union all 不去重
SQL26 计算25岁以上和以下的用户数量
题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
第一种方法:
select (case when age>=25 then '25岁及以上' else '25岁以下' end) age_cut,
count(device_id) as number from user_profile
group by age_Cut
CASE语句有两种形式:第一种评估一个或多个条件,并返回第一个符合条件的结果。 如果没有条件是符合的,则返回ELSE子句部分的结果,如果没有ELSE部分,则返回NULL:
第二种CASE句法返回第一个value = compare_value比较结果为真的结果。 如果没有比较结果符合,则返回ELSE后的结果,如果没有ELSE部分,则返回NULL:
第二种方法:
用IF
select if(age>=25,"25岁及以上","25岁以下") age_cut,
count(device_id) as number from user_profile
group by age_Cut
第三种方法:
用union all将两个 SQL 语句的结果合并在一起
select '25岁以下' as age_cut,count(device_id) as number
from user_profile
where age<25 or age is null
union all
select '25岁及以上' as age_cut,count(device_id) as number
from user_profile
where age>=25;
SQL27 查看不同年龄段的用户明细
题目:现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据。(注:若年龄为空请返回其他。)
select device_id,gender,
case
when age<20 then "20岁以下"
when age <25 then "20-24岁"
when age >=25 then '25岁及以上'
else "其他"
end age_cut
from user_profile
SQL28 计算用户8月每天的练题数量
题目:现在运营想要计算出2021年8月每天用户练习题目的数量,请取出相应数据。
select day(date) as day, count(question_id) as question_cnt
from question_practice_detail
where month(date)= 8
group by date
SQL29 计算用户的平均次日留存率
根据示例,你的查询应返回以下结果:
-
滞后一天日期且前一天上线的唯一id的总数量) / (前一天上线的唯一id的总数量),
-
用datediff区分第一天和第二天在线的device_id
-
用left outer join做自表联结
SELECT COUNT(distinct q2.device_id,q2.date)/count(DISTINCT q1.device_id,q1.date) as avg_ret
from question_practice_detail
as q1 left outer join question_practice_detail as q2
on q1.device_id=q2.device_id and DATEDIFF(q2.date,q1.date)=1
SQL30 统计每种性别的人数
题目:现在运营举办了一场比赛,收到了一些参赛申请,表数据记录形式如下所示,现在运营想要统计每个性别的用户分别有多少参赛者,请取出相应结果
SELECT right(profile,",",'-1') gender,count(device_id) number
from user_submit
group by gender
SQL31 提取博客URL中的用户名
- 题目:对于申请参与比赛的用户,blog_url字段中url字符后的字符串为用户个人博客的用户名,现在运营想要把用户的个人博客用户字段提取出单独记录为一个新的字段,请取出所需数据。
select
替换法 replace(string, '被替换部分','替换后的结果')
-- device_id, replace(blog_url,'http:/url/','') as user_name --80.09 --89.35
截取法 substr(string, start_point, length可选参数)--86.04 --87.07
-- device_id, substr(blog_url,11,length(blog_url)-10) as user_nam 80.09 --79.79
删除法 trim('被删除字段' from 列名)
-- device_id, trim('http:/url/' from blog_url) as user_name
字段切割法 substring_index(string, '切割标志', 位置数(负号:从后面开始))
device_id, substring_index(blog_url,'/',-1) as user_name --86.14--91.95
from user_submit;
补充知识:
-
1、LOCATE(substr , str ):返回子串 substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,则返回0;
-
2、POSITION(substr IN str ):返回子串 substr 在字符串 str 中第一次出现的位置,如果字符substr在字符串str中不存在,与LOCATE函数作用相同;
-
3、LEFT(str, length):从左边开始截取str,length是截取的长度;
-
4、RIGHT(str, length):从右边开始截取str,length是截取的长度;
-
5、SUBSTRING_INDEX(str ,substr ,n):返回字符substr在str中第n次出现位置之前的字符串;
-
6、SUBSTRING(str ,n ,m):返回字符串str从第n个字符截取到第m个字符;
-
7、REPLACE(str, n, m):将字符串str中的n字符替换成m字符;
-
8、LENGTH(str):计算字符串str的长度。
SQL32 截取出年龄
题目:现在运营举办了一场比赛,收到了一些参赛申请,表数据记录形式如下所示,现在运营想要统计每个年龄的用户分别有多少参赛者,请取出相应结果
SELECT SUBSTRING(profile,12,2) age,COUNT(device_id)
FROM user_submit
GROUP BY age;
最后
🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。