sql基础语句题练

此篇文章所有题都来源于牛客网

牛客网在线编程_SQL篇_非技术快速入门

记录一下自己的做题的历程,废话不多说直接开整

1.查询所有列

select * from user_profile

user_profile代表表table的意思

2.查询多列

select device_id,gender,age,university from user_profile;

3.查询结果去重

select distinct university from user_profile;

distinct的作用去除结果中重复的记录

如在university当中有两个北京大学的记录但咱们只想要一个没有重复的大学排列,就可以用distinct从而只会显示一个北京大学

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

select device_id from user_profile limit 0,2;

这个题用到了limit限制返回行数,上面就是返回device_id这个列的前两行

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

select device_id as user_infos_example from user_profile limit 0,2;

这里用as将device_id重新命名为user_infos_example

6.查找后排序

select device_id,age from user_profile order by age asc;

asc表示升序,desc表示降序。

7.查找后多列排序

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

order by默认按照升序排序

如果是降序语句如下

SELECT device_id, gpa, age 
FROM user_profile 
ORDER BY gpa DESC, age DESC;

8.查找后降序排列

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

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

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

利用where去进行条件定义

这里也介绍一下like,在这里用一下like做一下这道题

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

like就是模糊匹配

%表示匹配0个或多个字符

_表示只匹配一个字符

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

select device_id,gender,age,university from user_profile where age is not null and age>24;

and表示两边的条件要全部成立

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

select device_id,gender,age from user_profile where age between 20 and 23;

between and 表示一个区间

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

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

!=表示不等于

13.过滤空值

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

14.高级操作符练习

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

where条件和and的使用

15.高级操作符

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

and替换成or就行

16.where in 和Not in

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

17.操作符混合使用
 

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

就是and和or的混合使用

18.查找GPA的最高值

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

max最大值的使用

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

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

count是计算数量总数,avg表示平均数,as为命名,round表示保留几位小数,1就是表示留一位小数;

20.分组计算练习题

select gender,university,count(device_id) as user_num,avg(active_days_within_30) as avg_active_days,avg(question_cnt) as avg_question_cnt from user_profile group by gender,university;

count这里的聚合函数是关于group by后的内容进行聚合的及这个例子是根据性别和大学进行分类

21.分组过滤练习题

select university,round(avg(question_cnt),3) as avg_question_cnt,round(avg(answer_cnt),3) as avg_answer_cnt from user_profile group by university having avg_question_cnt<5 or avg_answer_cnt<20;

having是针对group by 进行过滤

22.分组排序练习题

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

先将所选出来的按照学校进行分组在按照升序进行排序

23.浙江大学用户题目回答

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;

用两个where条件,后一个where条件是用来确定device_id的范围是在浙江大学的范围内,然后进行排序

select qpd.device_id, qpd.question_id, qpd.result
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id=qpd.device_id and up.university='浙江大学'
order by question_id


inner join这里介绍一下这里它的作用,它是一个连接字符,后面加上成立的条件,如果成立的条件成立那么那个数据才会呈现出来.

24.统计每个学校的打过题的用户的平均答题数

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

distinct的作用就是count的一种作用就是去掉重复的,从而能计算出人数并且不重复。

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

select university,difficult_level,round(count(qpd.question_id) / count(distinct qpd.device_id), 4) as avg_answer_cnt
from question_practice_detail as qpdleft join user_profile as up
on up.device_id=qpd.device_idleft join question_detail as qd
on qd.question_id=qpd.question_idgroup by university, difficult_level

left join表连接的一种方式,会保持左表全部,而另一个表需要满足条件才能使用在最终的表中,如上面的例子当中第一个left join会保持qpd的全部内容,而up表中需要满足up.device_id=qpd.device_id这个条件。

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

select university,difficult_level,round(count(qpd.question_id)/count(distinct qpd.device_id),4) from question_practice_detail as qpd left join user_profile as up on up.device_id=qpd_device_id left join question_detail as qd on qd.question_id=qpd.question_id where university='山东大学' group by university,difficult_level;

和上一道题差不多就是多一个条件,大学为山东大学

27.查找山东大学或者为男生的信息

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 查询结果不去重,语句很好理解。

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

select case when age<25 or age is null then '25岁以下' when age>=25 then '25岁及以上' end age_cut,count(*)number from user_profile group by age_cut;

case的使用,这个使用很明显和c语言的差不多,end之后是对这一列的命名

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

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_cut from user_profile;

同上一道题一样的知识点

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

select day(date) as day,count(question_id) as question_cnt from question_practice_detail where month(date)=8 and year(date)=2021 group by date;

这里有几个日期的函数

day()提取日期中天那个属性

month() 提取日期中的月

year() 提取日期中的年的属性

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

select count(date2)/count(date1) as avg_ret from (select distinct qpd.device_id,qpd.date as date1,uniq_id_date.date as date2 from question_practice_detail as qpd left join(select  distinct device_id,date from question_practice_detail) as uniq_id_date on qpd.device_id=uniq_id_date.device_id and date_add(qpd.date,interval 1 day)=uniq_id_date.date) as id_last_next_date;

这道题就是通过device_id进行连接,计算第一天的人数,在计算第二天的人数,第二天的除以第一天的人数就是所要的概率

32.统计每种性别的人数

select if(profile like '%female','female','male') as gender,count(*) as number from user_submit group by gender;

用if进行判断,like模糊匹配。

这里在介绍一个字符串截取的函数substring_index(str,delim,count)

str是要截取的字符串,delim是分隔符,count是这个需要截取字符的位置,如这个例子sql语句就如下

SELECT SUBSTRING_INDEX(profile,",",-1) gender,COUNT(*) number
FROM user_submit 
GROUP BY gender;

33.截取出年龄

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

和上一道题一样多几个条件去使用就可以了

34.提取博客url中的用户名

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

还是使用substring_index()这个函数

35.找出每个学校GPA最低的同学

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;

加一个where条件去限制一下university和gpa就可以了

36.统计复旦用户八月练题情况

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;

多了一个sum函数其它的还是很好理解的

37.浙大不同难度题目正确率

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

这个题就是先计算出正确率然后利用device_id和question_id连接两个表最后在加条件分组排序;

38.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%";

这个题比较简单

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/52267.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[godot] 采用状态机时,如何处理攻击时移动?如“冲撞”

这里以‘史莱姆撞击’为例子&#xff0c;将‘空中跃进’定义为伤害帧。&#xff08;见下图&#xff09; 先梳理流程&#xff1a;a.史莱姆原地蓄力(起跳准备)--->b.跳起并移动一段距离(空中跃进)--->c.落地调整 一 当状态机进入‘攻击状态’时&#xff0c;在enter()中…

计算机毕业设计PySpark+Django农产品推荐系统 农产品爬虫 农产品商城 农产品大数据 农产品数据分析可视化 PySpark Hadoop

基于Spark的农产品个性推荐系统 相关技术介绍: 1. Python Python是一种高级编程语言&#xff0c;具有简洁、易读、易学的特点&#xff0c;被广泛应用于Web开发、数据分析、人工智能等领域。 在此系统中&#xff0c;我们使用Python进行后端开发&#xff0c;利用其强大的语法…

基本数据类型 --- 浮点型

float的机器码表示&#xff1a; 一个float数据 (pow(-1, sign) fraction) * pow(2, exponent - 127) 由上图&#xff0c;可得&#xff1a; (pow(-1, sign) fraction) * pow(2, exponent - 127) ( 1 2^(-2) ) * pow(2, 124-127) 0.15625 其他文章&#xff1a; https://b…

Docker Swarm部署SpringCloud Alibaba微服务踩坑记录

为了方便部署和维护微服务项目&#xff0c;还是得上集群部署方案&#xff0c;决定采用Docker的swarm&#xff0c;为什么不是k8s&#xff0c;因为部署骑来又是个新的工具&#xff0c;之前就一直用的docker&#xff0c;自带了类k8s的工具&#xff0c;索性就直接使用swarm了&#…

力扣(K件物品的最大和)

数据量小不需要考虑时间复杂度 数学思维理清楚步骤---然后代码翻译实现 Ⅰ、 K件物品的最大和 袋子中装有一些物品&#xff0c;每个物品上都标记着数字 1 、0 或 -1 。 给你四个非负整数 numOnes 、numZeros 、numNegOnes 和 k 。 袋子最初包含&#xff1a; numOnes 件标…

鸿蒙HarmonyOS之使用ArkTs语言实现层级树状目录选择UI

一、实现效果 二、实现步骤 代码示例中用到的颜色、图片等资源可以自行替换设置 1、Index.ets 里面调用 import { CategoryView} from ./CategoryView;//主页面 Entry Component struct Index {State tabsIndex: number 0;build() {...//层级目录ViewCategoryView()...} …

ReTagList标签列表(API)

组件实现基于 Vue3 + Element Plus + Typescript,同时引用 vueUse + lodash-es + tailwindCss (不影响功能,可忽略) 基于ElTag实现的Tag列表,支持Tag列表多选,动态Tag列表 ReTagList标签列表 基础 简单展示Tag列表,可通过size指定尺寸 查看 /demo/tag-list/basic.md …

Arduino开源四足蜘蛛机器人制作教程

视频教程&#xff1a;手把手叫你做四足蜘蛛机器人——1零件介绍_哔哩哔哩_bilibili 一、项目介绍 1.1 项目介绍 Arduino主控&#xff0c;图形化编程&#xff0c;趣味学习 Arduino nano开发板舵机扩展底板 4.8V可充电电池&#xff0c;支持Arduino C语言编程和米思齐图形化编程…

Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件

要使用 Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件&#xff0c;你可以按照以下步骤操作&#xff1a; ### 步骤 1: 添加依赖 首先&#xff0c;确保你的项目中添加了 Spring Cloud Config 客户端和 Bus 的依赖。对于 Maven 项目&#xff0c;pom.xml 文件应该…

浅谈Kafka(一)

浅谈Kafka&#xff08;一&#xff09; 文章目录 浅谈Kafka&#xff08;一&#xff09;Kafa的设计是什么样的数据传输的事务定义消息队列的应用场景Kafka怎么样判断节点是否存活Kafka的消息是采用pull模式还是push模式Kafka在磁盘上的消息格式Kafka高效文件存储设计特点Kafka与传…

Yolov10网络详解与实战(附数据集)

文章目录 摘要模型详解模型实战训练COCO数据集下载数据集 COCO转yolo格式数据集&#xff08;适用V4&#xff0c;V5&#xff0c;V6&#xff0c;V7&#xff0c;V8&#xff09;配置yolov10环境训练断点训练测试 训练自定义数据集Labelme数据集格式转换训练测试 总结 摘要 模型详解…

CeresPCL 岭回归拟合(曲线拟合)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 由于在使用最小二乘插值拟合时,会涉及到矩阵求逆的操作,但是如果这个矩阵接近于奇异时,那么拟合的结果就会与我们期望的结果存在较大差距,因此就有学者提出在最小二乘的误差函数中添加正则项,即: 这里我们也可…

OpenGL-ES 学习(8) ---- FBO

目录 FBO OverViewFBO 优点使用FBO的步骤 FBO OverView FBO(FrameBuffer Object) 指的是帧缓冲对象&#xff0c;实际上是一个可以添加缓冲区容器&#xff0c;可以为其添加纹理或者渲染缓冲区对象(RBO) FBO(FrameBuffer Object) 本身不能用于渲染&#xff0c;只有添加了纹理或者…

对派生类指针使用C风格的强制类型转换为基类指针

以下来自语言大模型的回答&#xff1a; 值可能发生改变 当对派生类指针使用C风格的强制类型转换成基类指针时&#xff0c;指针的值可能会发生改变。这主要是由于多重继承和虚继承的存在。以下是几个关键点解释: 单继承情况下: 在单继承的情况下&#xff0c;派生类指针转换为…

Stability AI发布了单目视频转4D模型的新AI模型:Stable Video 4D

开放生成式人工智能初创公司Stability AI在3月发布了Stable Video 3D&#xff0c;是一款可以根据图像中的物体生成出可旋转的3D模型视频工具。Stability AI在7月24日发布了新一代的Stable Video 4D&#xff0c;增添了赋予3D模移动作的功能。 Stable Video 4D能在约40秒内生成8…

数字乡村+智慧农业数字化转型大数据平台建设方案

1. 数字农业发展趋势 数字农业正经历全环节数字技术应用、全流程生产经营再造、全方位线上线下对接和管理服务全生命周期覆盖的四大趋势&#xff0c;标志着我国农业进入高质量发展新阶段。 2. 数字乡村的战略意义 数字乡村作为数字化、网络化和信息化的产物&#xff0c;对于…

Wemos D1 Mini pro/ nodeMcu / ESP8266 驱动 240*320 ILI9431 SPI液晶屏

Wemos D1 Mini / nodeMcu / ESP8266 驱动 240*320 ILI9431 SPI液晶屏 效果展示器件硬件连接引脚连接原理图引脚对照表 安装TFT_eSPI库TFT_eSPI库中User_Setup.h文件的参数修改User_Setup.h文件的位置User_Setup.h文件中需要修改的参数User_Setup.h完成源码 例程 缘起&#xff1…

PDF 转Word 开源库

1. Apache PDFBox Apache PDFBox 是一个开源的 Java 库&#xff0c;用于创建和操作 PDF 文档。虽然 PDFBox 本身没有直接支持 PDF 转 Word 的功能&#xff0c;但它可以提取 PDF 内容&#xff0c;你可以结合其他方法将这些内容写入 Word。 添加依赖 <dependency><gr…

网络间通信

1、udp通信 特点&#xff1a;&#xff08;1&#xff09;无连接 &#xff08;2&#xff09;不可靠 2、udp编程&#xff08;c/s模型&#xff09; ssize_t recvfrom(int sockfd, //socket的fd void *buf, //保存数据的一块空间的地址 …