Mysql练习题目【7月10日更新】

七、Mysql练习题目

https://zhuanlan.zhihu.com/p/38354000

1. 创建表

创建学生表
mysql> create table if not exists student(
    -> student_id varchar(255) not null,
    -> student_name varchar(255) not null,
    -> birthday date not null,
    -> gender varchar(255) not null,
    -> primary key(student_id)
    -> )default charset utf8;
创建成绩表
mysql> create table score(
  	-> student_id varchar(255) not null,
  	-> course_id varchar(255) not null,
  	-> score float(3) not null,
  	-> primary key(student_id,course_id)
		-> )default charset utf8;
创建课程表
mysql> create table course(
    -> course_id varchar(255) not null,
    -> course_name varchar(255) not null,
    -> teacher_id varchar(255) not null,
    -> primary key(course_id)
    -> )default charset utf8;
创建教师表
mysql> create table teacher(
  	-> teacher_id varchar(255) not null, 
  	-> teacher_name varchar(255) null, 
  	-> primary key(teacher_id) 
		-> ) default charset utf8;

2. 插入数据

学生表插入数据
insert into student
values
('0001','猴子','1989-01-01','男'),
('0002','猴子','1990-12-21','女'),
('0003','马云','1991-12-21','男'),
('0004','王思聪','1990-05-20','男');
成绩表插入数据
insert into score
values
('0001','0001',80),
('0001','0002',90),
('0001','0003',99),
('0002','0002',60),
('0002','0003',80),
('0003','0001',80),
('0003','0002',80),
('0003','0003',80);
课程表插入数据
insert into course
values
('0001','语文','0002'),
('0002','数学','0001'),
('0003','英语','0003');
教师表插入数据
insert into teacher 
values
('0001','孟扎扎'),
('0002','马化腾'),
('0003',null),
('0004','');

3. 题目

1.查询姓“猴”的学生名单
select * from student where student_name like "猴%";
2.查询名字中最后一个字是猴的学生名单
select * from student where student_name like "%猴";
3.查询名字中带猴的学生名单
select * from student where student_name like "%猴%";
4.查询名字中第二个字是猴的学生名单
select * from student where student_name like "_猴%";
5.查询姓“孟”老师的个数

关键字:个数-count

select count(*) from teacher where teacher_name like "孟%";
6.查询课程编号为“0002”的总成绩

关键字:总成绩-sum

select sum(score) from score where course_id='0002';
7.查询选了课程的学生人数*

关键字:人数-count

select 学号,成绩表里学号有重复值需要去掉

select count(distinct student_id) as 学生人数 from score;
8.查询各科成绩最高和最低的分

关键字:各科-分组;最高分-max ;最低分-min

select course_id,min(score) as 最低分,max(score) as 最高分 from score group by course_id;
9.查询每门课程被选修的学生数

关键字:每门-分组;学生数-count

select course_id as 课程,count(student_id) as 学生个数 from score group by course_id;
10.查询男生、女生人数

关键字:男生、女生(相当于每,因为性别只有男、女)-分组;人数-count

select gender,count(*) from student group by gender;
11.查询平均成绩大于60分学生的学号和平均成绩

关键字:平均成绩大于-having avg()>

select student_id,avg(score) from score group by student_id having avg(score)>60;
12.查询至少选修两门课程的学生学号

关键字:至少-count

select student_id from score group by student_id having count(course_id)>=2;
13.查询同名同姓学生名单并统计同名人数*

关键字:人数-count

select student_name,count(student_id) from student group by student_name having count(student_id) >=2; 
14.查询不及格的课程并按课程号从大到小排列
select * from score where score<60 order by course_id desc;
15.查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

关键字:每门-group by;平均成绩-avg

select course_id,avg(score) as 平均成绩 from score group by course_id order by 平均成绩,course_id desc;
16.检索课程编号为“0004”且分数小于60的学生学号,结果按按分数降序排列
select student_id student from score where course_id='0004' and score<60 order by score desc;
17.统计每门课程的学生选修人数(超过2人的课程才统计)

关键字:每门-group by;人数-count

select course_id,count(student_id) from score group by course_id having count(student_id)>2;
18.查询两门以上不及格课程的同学的学号及其平均成绩*

19.查询每个学生的总成绩并进行排名

关键字:每个-group by;总成绩-sum

select student_id,sum(score) as 总成绩 from score group by student_id order by 总成绩;
20.查询平均成绩大于60分的学生的学号和平均成绩

关键字:平均成绩大于-having avg()>60

select student_id,avg(score) as 平均成绩 from score group by student_id having avg(score)>60;
21.查询所有课程成绩小于60分学生的学号、姓名

关键字:所有成绩小于60-where score < 60

因为姓名在student表中,成绩在score表中,所以要用到子查询。

注意:如果(值1,值2,……)存在重复值时,in (值1,值2,……) 会从(值1,值2,……)中的重复值中选择一个。即in会过滤掉重复数据

select student_id,student_name from student where student_id in (select student_id from score where score<60);
22.查询没有学全所有课的学生的学号、姓名*
select student_id,student_name from student where student_id in (select student_id from score group by student_id having count(course_id) < (select count(*) from course));
23.查询出只选修了两门课程的全部学生的学号和姓名
select student_id,student_name from student where student_id in (select student_id from score group by student_id having count(course_id)=2);
24.查询各科成绩前两名的记录*

关键字:各科-group by


https://mp.weixin.qq.com/s/MuxjlFV0gi1GydOrYfiSeQ

img
25.查找1990年出生的学生名单*
select * from student where year(birthday) = 1990;select * from student where birthday like '1990-%';
26.查询各学生的年龄(精确到年)*
select student_id, year(current_time)-year(birthday)+1 from student;
27.查询各学生的年龄(精确到月份)*
select student_id,timestampdiff(month ,birthday ,now())/12 from student;
28.查询本月过生日的学生*
select * from student where month(birthday)=month(current_date);
29.查询所有学生的学号、姓名、选课数、总成绩*

要显示的列:学号,姓名,选课数,总成绩,分布在两个表中,所以应该用连表查询,将两个表连接起来

关键字:所有学生-左连接或右连接:左连接的话join左边的表应该为student,右连接的话join右边应该为student表。

注意:分组时,如果要用外连接的条件分组,则选择的那个表中的列中的数据应该是唯一的,及s.student_id

select s.student_id as 学号,s.student_name as 姓名,count(c.course_id) as 选课数,sum(c.score) as 总成绩 from student as s left join score as c on s.student_id=c.student_id group by s.student_id;
30.查询平均成绩大于85的所有学生的学号、姓名和平均成绩*
select t1.student_id as '学号',t1.student_name as '姓名',avg(score) as '平均成绩' from student as t1 left join score as t2 on t1.student_id = t2.student_id group by  t1.student_id having avg(t2.score)>85;
31.查询学生的选课情况:学号,姓名,课程号,课程名称*
select t1.student_id,t1.student_name,t3.course_id,t3.course_name from student as t1 left join score as t2 on t1.student_id=t2.student_id left join course as t3 on t2.course_id=t3.course_id;
32.查询出每门课程的及格人数和不及格人数*

关键字:及格和不及格-分类:case语句

select course_id, sum(case when score>=60 then 1 else 0 end) as 及格人数, sum(case when score<60 then 1 else 0 end) as 不及格人数 from score group by course_id;
33.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计:各分数段人数,课程号和课程名称
select a.课程号,b.课程名称,
sum(case when 成绩 between 85 and 100 
	 then 1 else 0 end) as '[100-85]',
sum(case when 成绩 >=70 and 成绩<85 
	 then 1 else 0 end) as '[85-70]',
sum(case when 成绩>=60 and 成绩<70  
	 then 1 else 0 end) as '[70-60]',
sum(case when 成绩<60 then 1 else 0 end) as '[<60]'
from score as a right join course as b 
on a.课程号=b.课程号
group by a.课程号,b.课程名称;
34.查询课程编号为0003且课程成绩在80分以上的学生的学号和姓名
select student_id,student_name from student where student_id in (select student_id from score where course_id='0003' and score>80);
35.检索"0001"课程分数小于60,按分数降序排列的学生信息
select t1.*,t2.score from student as t1 left join score as t2 on t1.student_id=t2.student_id where t2.course_id = 0001 and t2.score>60 order by t2.score desc;
36.查询不同老师所教不同课程平均分从高到低显示**

注意:select只能是聚合函数或group by后面的字段,否则会报SELECT list is not in GROUP BY clause and contains nonaggregated column的错误。原因是sql_mode模式的限制。可以修改这个模式,修改方法:https://blog.csdn.net/weixin_42085125/article/details/115335503

因为我们要查询t2.course_id,所以要在group by后面加上t2.course_id

select t1.teacher_name,t2.course_id,avg(t3.score) from teacher as t1 inner join course as t2 on t1.teacher_id=t2.teacher_id inner join score as t3 on t2.course_id=t3.course_id group by t1.teacher_id,t2.course_id order by avg(score) desc; 
37.查询课程名称为"数学",且分数低于60的学生姓名和分数
行列如何互换

sql面试题:行列如何互换?

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

算法金 | 12 个最佳 Python 代码片段,帮我完成工作自动化,香~

​大侠幸会幸会&#xff0c;我是日更万日 算法金&#xff1b;0 基础跨行转算法&#xff0c;国内外多个算法比赛 Top&#xff1b;放弃 BAT Offer&#xff0c;成功上岸 AI 研究院 Leader&#xff1b; Python是一种多功能的编程语言&#xff0c;它提供了各种功能和库来有效地自动化…

【分布式系统管理框架】Zookeeper集群

1、Zookeeper &#xff08;1&#xff09;Zookeeper定义 Zookeeper是一个开源的&#xff0c;为分布式框架提供协调服务的Apache项目。 &#xff08;2&#xff09;Zookeeper工作机制 Zookeeper从设计模式角度来理解&#xff1a;是一个基于观察者模式设计的分布式服务管理框架…

使用linux的mail命令发送html格式的邮件

1、关闭本机的sendmail服务或者postfix服务 #执行下面的命令&#xff0c;各位大侠都对号入座吧 #sendmial service sendmail stop chkconfig sendmail off #postfix service postfix stop chkconfig postfix off#再狠一点就直接卸载吧.. yum remove sendmail yum remove postf…

大小端详解

引例 我们知道整形(int)是4个字节&#xff0c;例如随便举个例子&#xff1a;0x01020304&#xff0c;它一共占了四个地址位&#xff0c;01,02,03,04分别占了一个字节&#xff08;一个字节就对应了一个地址&#xff09;。 那么就会有个问题&#xff1a;我们的01到底是存储在高地…

20_Inception V3深度学习图像分类算法

回顾GoogleNet:传送门 1.1 介绍 InceptionV3是Google开发的一种深度卷积神经网络架构&#xff0c;它是Inception系列网络中的第三代模型&#xff0c;由Christian Szegedy等人在论文《Rethinking the Inception Architecture for Computer Vision》中提出&#xff0c;该论文发…

在vue3中,手写父子关联,勾选子级父级关联,取消只取消当前子级,父节点不动

树形控件选择子级勾选父级&#xff0c;以及所有子级&#xff0c; 取消勾选仅取消子级 在项目中&#xff0c;可能会遇到这种场景&#xff0c;比如权限配置的时候&#xff0c;页面权限和菜单权限以tree的形式来配置&#xff0c;而且不用半选&#xff0c;菜单在页面的下面&#xf…

基于SpringBoot构造超简易QQ邮件服务发送 第二版

目录 追加 邮箱附件 添加依赖 编码 测试 第二版的更新点是追加了 邮箱附件功能 ( 后期追加定时任务 ) 基于SpringBoot构造超简易QQ邮件服务发送(分离-图解-新手) 第一版 追加 邮箱附件 添加依赖 <!-- 电子邮件 --><dependency><groupId>org.spri…

影视行业的人工智能与-【机器学习】:案例分析

欢迎关注小知&#xff1a;知孤云出岫 目录 引言AI和ML在影视行业的当前应用AI和ML对影视行业的未来影响案例研究&#xff1a;AI生成动画视频目标工具和库数据收集模型训练视频生成 结论参考文献 引言 人工智能&#xff08;AI&#xff09;和机器学习&#xff08;ML&#xff09…

java如何实现一个死锁 ?

死锁(Deadlock)是指在并发系统中,两个或多个线程(或进程)因争夺资源而互相等待,导致它们都无法继续执行的一种状态。 一、简易代码 public class DeadlockExample {private static final Object lock1 = new Object();private

Сетунь的24条单播指令

1、Setun模拟器概述 真的&#xff0c;想搞懂一台电脑是怎么运行的&#xff0c;那就搞懂它的指今集是怎么跑的&#xff0c;感觉很离了个大谱的&#xff0c;先看由铁氧体磁芯上的器件组成的RAM&#xff0c;容量为162个9-trit单元&#xff0c;即每个单元为9-trit&#xff0c;每页有…

Kubelet 认证

当我们执行kubectl exec -it pod [podName] sh命令时&#xff0c;apiserver会向kubelet发起API请求。也就是说&#xff0c;kubelet会提供HTTP服务&#xff0c;而为了安全&#xff0c;kubelet必须提供HTTPS服务&#xff0c;且还要提供一定的认证与授权机制&#xff0c;防止任何知…

【PB案例学习笔记】-30动态打开窗口

写在前面 这是PB案例学习笔记系列文章的第30篇&#xff0c;该系列文章适合具有一定PB基础的读者。 通过一个个由浅入深的编程实战案例学习&#xff0c;提高编程技巧&#xff0c;以保证小伙伴们能应付公司的各种开发需求。 文章中设计到的源码&#xff0c;小凡都上传到了gite…

Java | Leetcode Java题解之第225题用队列实现栈

题目&#xff1a; 题解&#xff1a; class MyStack {Queue<Integer> queue;/** Initialize your data structure here. */public MyStack() {queue new LinkedList<Integer>();}/** Push element x onto stack. */public void push(int x) {int n queue.size();…

用PlantUML和语雀画UML类图

概述 首先阐述一下几个简单概念&#xff1a; UML&#xff1a;是统一建模语言&#xff08;Unified Modeling Language&#xff09;的缩写&#xff0c;它是一种用于软件工程的标准化建模语言&#xff0c;旨在提供一种通用的方式来可视化软件系统的结构、行为和交互。UML由Grady…

HTML 标签简写和全称及其对应的中文说明和实例

<!DOCTYPE html> <html lang"zh-CN"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>HTML 标签简写及全称</title><style>…

WSL2编译使用6.6版本内核

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、有什么变化二、下载6.6内核三、开始编译1.安装环境2.开始编译 四、使用1.杀死虚拟机2.防止内核文件3.修改配置文件 总结 前言 最近出了一件不大不小的事&a…

Collection 和 Collections 的区别与用法

Collection 和 Collections 的区别与用法 1、Collection 接口1.1 主要特点1.2 常见方法 2、 Collections 工具类2.1 主要特点2.2 常见方法 3、示例代码3.1 使用 Collection 接口3.2 使用 Collections 工具类 4、总结 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收…

PostgreSQL 里怎样解决多租户数据隔离的性能问题?

文章目录 一、多租户数据隔离的性能问题分析&#xff08;一&#xff09;大规模数据存储和查询&#xff08;二&#xff09;并发访问和锁争用&#xff08;三&#xff09;索引维护成本高&#xff08;四&#xff09;资源分配不均 二、解决方案&#xff08;一&#xff09;数据分区&a…

神经网络构成、优化、常用函数+激活函数

Iris分类 数据集介绍&#xff0c;共有数据150组&#xff0c;每组包括长宽等4个输入特征&#xff0c;同时给出输入特征对应的Iris类别&#xff0c;分别用0&#xff0c;1&#xff0c;2表示。 从sklearn包datasets读入数据集。 from sklearn import darasets from pandas impor…

申请商标用什么颜色:企业和个人申请注册商标攻略!

在申请注册商标到底要用什么颜色&#xff0c;许多初次申请注册主体都不是特别清楚&#xff0c;普推知产商标老杨建议&#xff0c;在一般情况下建议尽量用黑白色&#xff0c;因为商标用黑白色在使用时可以着任何色。 在用黑色申请注册成功&#xff0c;别的主体用其它颜色要在同…