牛客SQL刷题第一趴——非技术入门基础篇

id device_id gender age university province 1 2138 male 21 北京大学 Beijing 2 3214 male 复旦大学 Shanghai 3 6543 female 20 北京大学 Beijing 4 2315 female 23 浙江大学 ZheJiang 5 5432 male 25 山东大学 Shandong

id device_id question_id result date 1 2138 111 wrong 2021-05-03 2 3214 112 wrong 2021-05-09 3 3214 113 wrong 2021-06-15 4 6543 111 right 2021-08-13 5 2315 115 right 2021-08-13 6 2315 116 right 2021-08-14 7 2315 117 wrong 2021-08-15

question_id difficult_level 111 hard 112 medium 113 easy 115 easy 116 medium 117 easy

一、基础查询

标题:现在操作员要查看用户信息表中的所有数据,请取出相应的结果

[En]

Topic: now the operator wants to view all the data in the user information table, please take out the corresponding results

题目:现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据

主题:现在运营商需要检查用户来自哪些学校。请从用户信息表中取出已消除重复数据的学校数据。

[En]

Topic: now the operator needs to check which schools the users come from. Please take out the deduplicated data of the school from the user information table.

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

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

二、条件查询

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

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

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

主题:现在运营商想要查看除复旦大学以外的所有用户的详细信息。请拿出相应的数据。

[En]

Topic: now the operator wants to check the details of all users except Fudan University. Please take out the corresponding data.

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

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

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

题目:现在运营商想找学校为北京大学、复旦大学、盛大的学生进行调研。请把相关数据拿出来。

[En]

Topic: now the operator wants to find the school to conduct research for the students of Peking University, Fudan University and Shanda University. Please take out the relevant data.

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

话题:现在运营商想要查看北京所有高校的用户信息,请拿出相应的数据。

[En]

Topic: now the operator wants to view the information of users in all universities with Beijing, please take out the corresponding data.

主题:现在运营商想拿出用户信息表中用户的年龄,请拿出相应的数据,按年龄升序进行排序。

[En]

Topic: now that the operator wants to take out the user’s age in the user information table, please take out the corresponding data and sort them in ascending order of age.

三、高级查询

题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据

题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。

话题:现在运营方想要分析每个学校不同性别的用户活跃度和发帖数量,请计算每个学校每个性别的用户数量、平均活跃天数和30天内的平均发帖数量。

[En]

Topic: now the operator wants to analyze the user activity and the number of posts of different genders in each school, please calculate the number of users of each gender in each school, the average number of active days and the average number of posts in 30 days.

用户信息表:user_profile

30天内活跃天数字段(active_days_within_30)

发帖数量字段(question_cnt)

回答数量字段(answer_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;

SQL19 分组过滤练习题

话题:现在运营者想要查看每个学校用户的平均发帖和回复,寻找低活跃度的学校进行重点操作,请去掉平均发帖数不到5条的学校或平均回复数不到20条的学校。

[En]

Topic: now the operator wants to check the average posts and responses of each school user, looking for low-activity schools for key operations, please take out schools with an average number of posts less than 5 or schools with an average number of responses less than 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 分组排序练习题

标题:现在运营商希望查看用户在不同大学的平均发帖情况,并希望结果按照平均发帖情况按升序进行排序。请拿出相应的数据。

[En]

Topic: now the operator wants to check the average posting of users in different universities, and expects the results to be sorted in ascending order according to the average posting. Please take out the corresponding data.

SELECT university,AVG(question_cnt) AS avg_question_cnt
FROM user_profile
GROUP BY university
ORDER BY avg_question_cnt;

四、多表查询

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

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

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

运营商想知道每所学校用户平均回答的问题数量,请把数据拿出来。

[En]

Operators want to know the average number of questions answered by users in each school, please take out the data.

请你写SQL查找每个学校用户的平均答题数目(说明:某学校用户平均答题数量计算方式为该学校用户答题总次数除以答过题的不同用户个数)根据示例,你的查询应返回以下结果(结果保留4位小数),注意:结果按照university升序排序!!!

用户信息表 user_profile,其中device_id指终端编号(认为每个用户有唯一的一个终端),gender指性别,age指年龄,university指用户所在的学校,gpa是该用户平均学分绩点,active_days_within_30是30天内的活跃天数。

select u.university,ROUND(count(q.question_id)/count(DISTINCT(q.device_id)),4)
from user_profile u
join question_practice_detail q
on u.device_id=q.device_id
group by 1
order by 1;

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

题目:运营想要计算一些参加了答题的不同学校、不同难度的用户平均答题量,请你写SQL取出相应数据

请你写一个SQL查询,计算不同学校、不同难度的用户平均答题量,根据示例,你的查询应返回以下结果(结果在小数点位数保留4位,4位之后四舍五入):

select u.university,
q2.difficult_level,
ROUND(count(u.question_cnt)/count(distinct(u.device_id)),4) as avg_answer_cnt
from user_profile u
join question_practice_detail q1
on u.device_id=q1.device_id
join question_detail q2
on q1.question_id=q2.question_id
group by 1,2;

ps:用户平均答题量 = 答题总数 / 用户数,要明确答题总数需要用question_id的行数来表示,也就是需对question_id列进行计数,用户数是device_id的行数,其中答题的题目可以重复,用户则不能重复计数

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

题目:运营商想查看山东大学用户在不同困难情况下的平均答题次数,请拿出相应的数据

[En]

Topic: operators want to check the average number of questions answered by users of Shandong University under different difficulties, please take out the corresponding data

请你写一个SQL查询,计算山东、不同难度的用户平均答题量,根据示例,你的查询应返回以下结果(结果在小数点位数保留4位,4位之后四舍五入):

select u.university,
q2.difficult_level,
ROUND(count(u.question_cnt)/count(DISTINCT(u.device_id)),4) as avg_answer_cnt
from user_profile u
join question_practice_detail q1
on u.device_id=q1.device_id
join question_detail q2
on q1.question_id=q2.question_id
where u.university='山东大学'
group by 1,2;

解题思路:1⃣️多表连接

2⃣️用户平均答题量 = 答题总数 / 用户数,要明确答题总数需要用question_id的行数来表示,也就是需对question_id列进行计数,用户数是device_id的行数,其中答题的题目可以重复,用户则不能重复计数

3⃣️where子句筛选university为山东大学。

4⃣️group by 按照大学分组。

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';

五、必会的常用函数

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

题目:现在运营商想把用户分为25岁以下和25岁以上两个年龄段,分别检查这两个年龄段的用户数。

[En]

Topic: now operators want to divide users into two age groups: under 25 and 25 and above, and check the number of users in these two age groups respectively.

本题注意:age为null 也记为 25岁以下

select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cut,
count(DISTINCT(device_id)) as number
from user_profile
group by 1;

解题思路:1⃣️case when,或者if函数都可以。

2⃣️有聚合函数要用group by。

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

主题:现在运营商希望将用户分为三个年龄段:20岁以下、20-24岁、25岁以上。查看不同年龄段用户的详细信息。请拿出相应的数据。(注:如年龄为空,请返回其他。)

[En]

Topic: now operators want to divide users into three age groups: under 20, 20-24, 25 and above. Check the details of users in different age groups. Please take out the corresponding data. (note: if the age is empty, please return to other. )

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

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

题目:现在运营商要计算2021年8月每天的用户练习题数量,请拿出相应的数据。

[En]

Topic: now that the operator wants to calculate the number of user practice questions per day in August 2021, please take out the corresponding data.

select DAY(date),
count(question_id) as question_cnt
from question_practice_detail
where date>='2021-08-01' and date'2021-8-31'
group by 1;

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

主题:现在运营商希望看到用户在做完一天练习后第二天回来的平均概率。请拿出相应的数据。

[En]

Topic: now the operator wants to see the average probability that users will come back the next day after doing exercises one day. Please take out the corresponding data.

select avg(if(b.device_id is not null,1,0)) as avg_ret
from(select distinct device_id,date
    from question_practice_detail)a
    left join
    (select distinct device_id,date_sub(date,interval 1 day) as date
    from question_practice_detail)b
    on a.device_id=b.device_id and a.date=b.date;

SQL30 统计每种性别的人数

题目:现在运营商已经举办了一场大赛,并收到了一些参赛申请。数据记录的形式如下。现在运营商要统计的是各性别用户的参赛人数,请拿出相应的结果。

[En]

Topic: now the operator has held a competition and received some applications for the competition. The form of data recording is as follows. Now the operator wants to count the number of contestants for users of each gender, please take out the corresponding results.

select substring(profile,15,6) as gender,count(device_id) as number
from user_submit
group by 1;

SQL31 提取博客URL中的用户名

题目:对于申请参与比赛的用户,blog_url字段中url字符后的字符串为用户个人博客的用户名,现在运营想要把用户的个人博客用户字段提取出单独记录为一个新的字段,请取出所需数据。

select device_id,
substring(blog_url,11,11) as user_name
from user_submit;

SQL32 截取出年龄

题目:现在运营商已经举办了一场大赛,并收到了一些参赛申请。表格数据的记录形式如下。现在运营商想要为每个年龄段的用户统计参赛者的数量。请拿出相应的结果。

[En]

Topic: now the operator has held a competition and received some applications for the competition. The record form of the table data is as follows. Now the operator wants to count the number of contestants for users of each age. Please take out the corresponding results.

select substring(profile,12,2) as age,
count(device_id) as number
from user_submit
group by 1;

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

题目:现在运营想要找到每个学校gpa最低的同学来做调研,请你取出每个学校的最低gpa。

根据示例,你的查询结果应参考以下格式,输出结果按university升序排序:

-- 方法1:join
select u.device_id,u.university,u.gpa
from user_profile u
join (select university,min(gpa) as gpa from user_profile group by university) u1
on u.university=u1.university and u.gpa=u1.gpa
order by university;

-- 方法2:select子查询
select device_id,university,gpa
from user_profile
where (university,gpa) in(select university,min(gpa) from user_profile
                       group by university)
order by university;

解题思路:也可以使用窗口函数。

[En]

Problem-solving ideas: you can also use window functions.

六、综合练习

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

题目: 现在运营想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0.

select u.device_id,u.university,
sum(if(question_id is not NUll,1,0)) as question_cnt,
sum(if(result='right',1,0)) as right_question_cnt
from user_profile u
left join question_practice_detail q
on u.device_id=q.device_id and month(date)=8
where u.university='复旦大学'
group by 1,2;

解题思路:1⃣️多表连接,使用left join

2⃣️关于日期函数写在where中会报错。

关于month(date)为什么不写在where后面:首先month函数不是聚合函数,是可以写在where语句中的;其次是,如果写在where中,是对连接好的表进行判断,如果是用user表leftjoin question表,由于question表里没有4321用户的记录,那么连接好的表中的id为4321的用户是没有date值的,也就是date为空,所以在执行where month(date)=8的时候会除掉4321这行记录,所以最后的结果里就没有这个id的记录啦!主要是要理解:1.先执行from,再执行where,where中的操作是对连接好的表的操作;2.a左连接b,对于a有而b没有的id,则连接好的表中的这些id的b相关的属性值为空。

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

话题:现在运营商想知道浙江大学用户在不同难度题下的答题正确率,请取出相应的数据按准确度升序输出。

[En]

Topic: now the operator wants to know the correct rate of questions answered by users of Zhejiang University under different difficulty questions, please take out the corresponding data and output them in ascending order of accuracy.

select q.difficult_level,
sum(if(q1.result='right',1,0))/count(q1.question_id) as correct_rate
from question_detail q
left join question_practice_detail q1
on q.question_id=q1.question_id
left join user_profile u
on q1.device_id=u.device_id
where u.university='浙江大学'
group by 1
order by 2;

解题思路:1⃣️多表连接

2⃣️正确率的计算公式:回答正确的个数/回答的总题目

SQL39 21年8月份练题总数

题目: 现在运营想要了解2021年8月份所有练习过题目的总用户数和练习过题目的总次数,请取出相应结果

select count(DISTINCT(device_id)) as did_cnt,count(question_id) as question_cnt
from question_practice_detail
where date>='2021-08-01 00:00:00' and date'2021-08-31 23:59:59';

Original: https://www.cnblogs.com/ruoli-121288/p/16378075.html
Author: 徐若离
Title: 牛客SQL刷题第一趴——非技术入门基础篇

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505280/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • windows与linux查看文件md5值

    windows CertUtil -hashfile FilePath MD5 linux md5sum FilePath Original: https://www.cnblog…

    数据库 2023年6月11日
    088
  • MySQL Bug:No suitable ‘keyring_component_metadata_query’ service implementation found to fulfill the request

    监控发现公司一台MySQL 8.0.26 的错误日志中出现大量下面告警信息: [Warning] [MY-013712] [Server] No suitable ‘k…

    数据库 2023年5月24日
    086
  • 模板语法之过滤器

    过滤器的作用 用来修改变量的输出结果 语法 {{变量名|过滤器1:’参数值1’|过滤器2:’参数值2}} <h1>&#x8F…

    数据库 2023年6月14日
    095
  • Linux平台Redis安装总结

    本文测试验证的操作系统为CentOS Linux release 7.8.2003 (Core),Redis版本为redis-6.0.8。 敬请注意,如有不同,请以实际情况为准。 …

    数据库 2023年6月11日
    063
  • centos8 安装python

    镜像:CentOS Linux release 8.5.0-13 python下载地址:Python Source Releases | Python.org 选择所需要的版本,我…

    数据库 2023年6月11日
    0110
  • MySQL启动报:[ERROR] The server quit without updating PID file

    修改配置后 MySQL启动不了,报错: 看见这个不要惊慌,先把刚才修改的配置注释掉,看是不是配置有误!大部分是手误造成。 如果不行,再尝试一下方法: 解决方法 : 给予权限,执行 …

    数据库 2023年6月14日
    0100
  • 【Kubernetes系列】Kubernetes相关概念介绍

    Pod 是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元。是一组(一个或多个) 容器; 这些容器共享存储、网络、以及怎样运行这些容器的声明。 Pod 中的内容…

    数据库 2023年6月6日
    085
  • 890.查找和替换模式

    你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配。 如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之…

    数据库 2023年6月16日
    078
  • Windows安装mysql数据库

    一般我安装mysql用以下两个方法: 一.phpstudy环境下的mysql安装 只需将mysql的bin目录配置到系统环境变量即可, 输入默认密码root即可登录 二.本地直接安…

    数据库 2023年6月16日
    071
  • 事务的7种传播行为

    转载:https://blog.csdn.net/soonfly/article/details/70305683 事务传播行为 事务传播行为指的是当一个事务方法被另一个事务方法调…

    数据库 2023年6月6日
    0116
  • Python 垃圾回收总结

    前言 最近在阅读《垃圾回收的算法与实现》,里面将讲到了一些常用的垃圾回收(Garbage Collect)算法,如:标记-清除、引用计数、分代回收等等。后面讲到了 Python 的…

    数据库 2023年6月6日
    0109
  • 工作中常用Less知识点实践总结

    工作中常用Less知识点实践总结,帮助你更好的使用Less 我所理解的Less的一些好处 函数式编程css 自定义变量用于整体主题调整 嵌套语法简化开发复杂度 mixin的写法 ….

    数据库 2023年6月11日
    087
  • Consul 入门-初识

    背景 现状:单体架构逐渐被微服务架构所替代,原本两个功能模被拆分成了两个服务。原本两个模块块间的通信只需要 &#x51FD;&#x6570;&#x8C03;…

    数据库 2023年6月6日
    092
  • [javaweb]监听器统计网页在线人数

    监听器 1.配置监听器 package com.javaweb.controller; import javax.servlet.ServletContext; import ja…

    数据库 2023年6月16日
    097
  • Java数据结构和算法

    一、数据结构 数据结构是计算机存储、组织数据的方式,指相互之间存在一种或多种特定关系的数据元素的集合。 通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同…

    数据库 2023年6月11日
    0101
  • Java-课堂笔记

    用Typora重新排版了下, 发现还有挺多问题, 手动排版好像也不识别. 太麻烦就这样了, 课堂笔记只是证明自己没有上课摸鱼, 这里的笔记是当初疫情上网课写的. 以后会按照路线重新…

    数据库 2023年6月11日
    086
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球