文章目录

  • 前言
  • ⛳️ 1.环境准备
  • ⛳️ 2.实训真题
    • 2.1 select基本用法
    • 2.2 聚合函数查询
    • 2.3 分组聚合查询
    • 2.4 多表连接查询
    • 2.5 子查询
    • 2.6 联合查询
  • 总结

前言

SQL每个人都要用,但是用来衡量产出的并不是SQL本身,你需要用这个工具,去创造其它的价值。


按照要求写答案,留在评论区哈,我会点评回复

⛳️ 1.环境准备

CREATE TABLE `tb_class` (`classNo` char(6) NOT NULL,`className` varchar(20) NOT NULL,`department` varchar(20) DEFAULT NULL,`grade` enum('1','2','3','4') DEFAULT NULL,`classNum` tinyint(4) DEFAULT NULL,PRIMARY KEY (`classNo`),UNIQUE KEY `uq_class` (`className`)
)INSERT INTO `tb_class`
VALUES ('AC1301','会计13-1班','会计学院','1',35),
('AC1302','会计13-2班','会计学院','1',35),
('CS1401','计算机14-1班','计算机学院','2',35),
('IS1301','信息系统13-1班','信息学院','1',NULL),
('IS1401','信息系统14-1班','信息学院',NULL,30);
CREATE TABLE `tb_course` (`courseNo` char(6) NOT NULL,`courseName` varchar(20) NOT NULL,`credit` decimal(3,1) NOT NULL,`courseHour` tinyint(2) NOT NULL,`term` tinyint(1) DEFAULT NULL,`priorCourse` char(6) DEFAULT NULL,PRIMARY KEY (`courseNo`),UNIQUE KEY `courseName` (`courseName`),UNIQUE KEY `uqidx_courseName` (`courseName`(3)),KEY `fk_course` (`priorCourse`),CONSTRAINT `fk_course` FOREIGN KEY (`priorCourse`) REFERENCES `tb_course` (`courseNo`)
)INSERT INTO `tb_course`
VALUES ('11003','管理学',2.0,32,2,NULL),
('11005','会计学',3.0,48,3,NULL),
('21001','计算机基础',3.0,48,1,NULL),
('21002','OFFICE高级应用',3.0,48,2,'21001'),
('21004','程序设计',4.0,64,2,'21001'),
('21005','数据库',4.0,64,5,'21004'),
('21006','操作系统',4.0,64,5,'21001'),
('31001','管理信息系统',3.0,48,3,'21004'),
('31002','信息系统-分析与设计',2.0,32,4,'31001'),
('31005','项目管理',3.0,48,5,'31001');
CREATE TABLE `tb_score` (`studentNo` char(10) NOT NULL,`courseNo` char(6) NOT NULL,`score` decimal(4,1) NOT NULL,PRIMARY KEY (`studentNo`,`courseNo`),KEY `idx_stuNo_courNo` (`courseNo`,`studentNo`),KEY `idx_courseNo` (`courseNo`),CONSTRAINT `fk_score_courNo` FOREIGN KEY (`courseNo`) REFERENCES `tb_course` (`courseNo`),CONSTRAINT `fk_score_stuNo` FOREIGN KEY (`studentNo`) REFERENCES `tb_student` (`studentNo`)
)INSERT INTO `tb_score` VALUES ('2013110101','11003',90.0),('2013110101','21001',86.0),
('2013110103','11003',89.0),('2013110103','21001',88.0),('2013110201','11003',78.0),
('2013110201','21001',92.0),('2013110202','11003',82.0),('2013110202','21001',85.0),
('2013310101','21004',83.0),('2013310101','31002',68.0),('2013310103','21004',80.0),
('2013310103','31002',76.0),('2014210101','11003',80.0),('2014210101','11005',75.0),
('2014210101','21001',60.0),('2014210101','21002',93.0),('2014210101','21004',89.0),
('2014210101','21005',55.0),('2014210101','21006',80.0),('2014210101','31001',68.0),
('2014210101','31002',77.0),('2014210101','31005',85.0),('2014210102','21002',95.0),
('2014210102','21004',88.0),('2014310101','21001',79.0),('2014310101','21004',80.0),
('2014310102','21001',91.0),('2014310102','21004',87.0);
CREATE TABLE `tb_student` (`studentNo` char(10) NOT NULL,`studentName` varchar(10) NOT NULL,`sex` char(2) DEFAULT NULL,`birthday` date DEFAULT NULL,`native` varchar(20) DEFAULT NULL,`nation` varchar(20) DEFAULT '汉',`classNo` char(6) DEFAULT NULL,PRIMARY KEY (`studentNo`),KEY `fk_student` (`classNo`),CONSTRAINT `fk_student` FOREIGN KEY (`classNo`) REFERENCES `tb_class` (`classNo`)
)INSERT INTO `tb_student`
VALUES ('2013110101','张晓勇','男','1997-12-11','山西','汉','AC1301'),
('2013110103','王一敏','女','1996-03-25','河北','汉','AC1301'),
('2013110201','江山','女','1996-09-17','内蒙','锡伯','AC1302'),
('2013110202','李明','男','1996-01-14','广西','壮','AC1302'),
('2013310101','黄菊','女','1995-09-30','北京','汉','IS1301'),
('2013310102','林海','男','1996-01-18','北京','满','IS1301'),
('2013310103','吴昊','男','1995-11-18','河北','汉','IS1301'),
('2014210101','黄涛','男','1997-04-03','湖南','侗','CS1401'),
('2014210102','郭志坚','男','1997-02-21','上海','汉','CS1401'),
('2014210103','王玲','女','1998-02-21','安徽','汉','CS1401'),
('2014310101','王林','男','1996-10-09','河南','汉','IS1401'),
('2014310102','李怡然','女','1996-12-31','辽宁','汉','IS1401'),
('2015310103','李彤','男',NULL,NULL,'傣','IS1401');

⛳️ 2.实训真题

2.1 select基本用法

1.查询所有班级的班级编号、所属学院和班级名称
select classNo,department,className from tb_class;2.从tb_class表中查询所有的学院名称。
select distinct department from tb_class;3.查询全体学生的详细信息
select * from tb_student;4.查询全体学生的姓名、性别和年龄
select studentName,sex,year(now())-year(birthday) age
from  tb_student;5.查询全体学生的姓名、性别和年龄,要求用汉语显示目标列表的名称。
select studentName as '姓名',
sex as '性别',
year(now())-year(birthday) as '年龄'
from  tb_student;6.查询课时大于等于48学时的课程名称和学分
select courseName,credit from tb_course
where courseHour >= 48;7.查询少数民族学生的姓名、性别、籍贯和民族
select studentName,sex,nation, native
from tb_student where  nation <> '汉';8.查询1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student
where year(birthday)=1997;9.查询不是1997年出生的学生姓名、性别和具体日期。
select studentName,sex,birthday
from tb_student where year(birthday)!=1997;10.查询籍贯是北京、天津和上海的学生信息。
select * from tb_student
where native in ('北京','天津','上海');11.查询籍贯不是北京、天津和上海的学生信息。
select * from tb_student
where native not in ('北京','天津','上海');12.查询2013年入学的学生全部信息。
select * from tb_student
where left(studentNo,4)='2013';13.查询所有姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like '王%';14.查询所有不姓王的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName not like '王%';15.   查询姓名中包含‘林’字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo
from tb_student where studentName like '%林%';16.查询姓王的且姓名为三个字的学生的学号、姓名和班级编号。
select studentNo,studentName,classNo from tb_student
where studentName like '王%' and CHAR_LENGTH(studentName)=3;17.查询课程名称中包含’-‘符号的课程信息;
select * from tb_course where courseName like '%-%';18.查询课程名称中带有中文‘系统’的课程信息。
select * from tb_course where courseName like '%系统%';19.查询课程名称中含有‘管理’、‘信息’或者‘系统’的课程信息。
select * from tb_course
where courseName like '%管理%' or courseName like '%信息%' or courseName like '%系统%';20.查询缺少先修课的课程信息。
select * from tb_course where priorCourse is null;21.查询所有有先修课的课程信息
select * from tb_course where priorCourse is not null;22.查询学分大于等于3且学时数大于32的的课程名称、学分和学时数。
select courseName,credit,courseHour
from tb_course
where credit=3 and courseHour>32;23.查询籍贯是北京或者上海的学生的姓名、籍贯和民族。
select  studentName,native, nation
from tb_student
where native in ('北京','上海');24.查询籍贯是北京或湖南的少数民族男生的姓名、籍贯和民族。
select  studentName,native, nation
from tb_student where native in ('北京','湖南')
and nation<>'汉';25.查询学生的姓名、籍贯和民族,并将查询结果按姓名升序排序。
select * from tb_student order by studentName;26.查询学生选课成绩大于85分的学号、课程号和成绩信息,并将查询结果先按学号升序排列,再按成绩降序排列。
select * from tb_score
where  score>85
order by studentNo,score desc;27.查询成绩排名第3至第5的学生学号、课程号和成绩
select * from tb_score
order by score desc limit 2,3;

2.2 聚合函数查询

28.查询学生总人数。
select count(*) from tb_student;29.查询选修了课程的学生总人数。
select sum(classNum) from tb_class;30.计算选修课程编号为‘21001’的学生平均成绩。
select avg(score) from tb_score where courseNo='21001';31.计算选修课程编号为‘21001’的学生最高分。
select max(score) from tb_score where courseNo='21001';

2.3 分组聚合查询

32.查询各个课程号以及相应的选课人数。
select courseNo,count(*) from tb_score group by courseNo;33.查询每个学生的选课门数、平均分和最高分
select studentNo,count(courseNo),avg(score),max(score)
from tb_score group by studentNo;34.查询平均分在80分以上的每个同学的选课门数、平均分和最高分。
select studentNo,count(courseNo),avg(score),max(score)
from tb_score group by studentNo having avg(score)>80;35.查询有2门以上(含2门)课程的成绩大于88分的学生学号及(88分以上的)课程数。
select studentNo,count(courseNo)
from tb_score where score> 88 group by studentNo
having count(courseNo)>=2;36.查询所有学生选课的平均成绩,但只有当平均成绩大于80的情况下才输出。
select studentNo,avg(score)
from tb_score group by studentNo having avg(score)>80;

2.4 多表连接查询

37.查询每个学生选修课程的情况
select b.studentNo,b.studentName,c.*
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo;38.查询会计学院全体同学的学号、姓名、籍贯、班级编号和所在班级名称。
select a.studentNo,a.studentName,a.native,a.classNo,b.className
from tb_student a,tb_class b
where a.classNo = b.classNo
and b.department ='会计学院';39. 查询选修了课程名称为‘程序设计’的学生学号、姓名和成绩。
select b.studentNo,b.studentName,a.score
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='程序设计';40.查询与数据库这门课学分相同的课程信息。
select * from tb_course a where a.credit in
(select credit from tb_course b
where a.credit=b.credit and b.courseName = '数据库')
and a.courseName <> '数据库';41.使用左连接查询所有学生及其选修课程的情况,
包括没有选修课程的学生,要求显示学号、姓名、性别、班号、选修的课程号和成绩。select a.studentNo,a.studentName,a.sex,a.classNo,c.courseName,b.score
from tb_student a left join tb_score b on a.studentNo = b.studentNo
join tb_course c on b.courseNo=c.courseNo;42.使用右连接查询所有学生及其选修课程的情况,
包括没有选修课程的学生,要求显示学号、姓名、性别、班号、选修的课程号和成绩。select a.studentNo,a.studentName,a.sex,a.classNo,c.courseName,b.score
from tb_student a right join tb_score b on a.studentNo = b.studentNo
join tb_course c on b.courseNo=c.courseNo;

2.5 子查询

43.查询选修了课程的学生姓名
select a.studentName from tb_student a where a.studentNo in
(select b.studentNo from tb_score b);44.    查询没有选修过课程的学生姓名。
select a.studentName from tb_student a where a.studentNo not in
(select b.studentNo from tb_score b);45.查询班级‘计算机14-1班’所有学生的学号和姓名。
select a.studentNo,a.studentName from tb_student a where a.classNo in
(select b.classNo from tb_class b where b.className ='计算机14-1班');46.查询与‘李明’同班的学生学号、姓名和班级编号。
select * from tb_student a
where a.classNo in
(select b.classNo from tb_student b where b.studentName='李明')
and a.studentName != '李明';47.查询男生中比任意一个女生出生年份都晚的学生姓名和出生年份。
select * from tb_student a where a.birthday > any
(select birthday from tb_student b where b.sex ='女')
and a.sex ='男';48.查询选修了课程号为‘31002’的学生姓名。
select a.studentName from tb_student a
where a.studentNo in
(select b.studentNo from tb_score b where b.courseNo='31002');49.查询没有选修课程号为‘31002’的学生姓名。
select a.studentName from tb_student a
where a.studentNo not in
(select b.studentNo from tb_score b where b.courseNo='31002');50.查询选修了全部课程的学生姓名
select a.studentName from tb_student a
where a.studentNo in (
select studentNo from tb_score group by studentNo
having count(*) = (select count(*) from tb_course)
);

2.6 联合查询

51.使用UNION查询选修了‘管理学’或者‘计算机基础’的学生学号
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='管理学'
union
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='计算机基础';52.使用UNION ALL查询选修了‘管理学’或者‘计算机基础’的学生学号。
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='管理学'
union all
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='计算机基础';53.查询选修了‘计算机基础’,但没有选修‘管理学’的学生学号。
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName='计算机基础'
union
select b.studentNo
from tb_score a,tb_student b,tb_course c
where a.studentNo = b.studentNo
and a.courseNo = c.courseNo
and c.courseName<>'管理学';

总结

主要是让大家实训操作,如果有更好的答案留在评论区哈

如何体系化学习SQL呢?查看以下博客
https://blog.csdn.net/weixin_41645135/category_11653817.html

如何体系化学习MySQL呢?
https://www.modb.pro/course/130

MySQL基本操作—DQL实训真题相关推荐

  1. mysql数据库实验实训5,数据查询yggl数据库查询(详细)

    数据库实验yggl数据库,实训5(详细版) 1.SELECT语句的基本使用: 1-1:查询employees 表的员工部门号和性别,要求消除重复行 select distinct '员工部门号','性 ...

  2. mysql期末实验实训装置厂家_消防广播电话系统实验实训装置

    一.设备参数及性能: 1.装置组成: 消防广播电话系统实验实训装置元器件全部采用实物,整个实训装置主要由录放单元.功放单元.广播切换模块.总线火警通讯盘.电话模块.多种消防电话.音箱.音源等组成.系统 ...

  3. mysql期末实验实训装置厂家_工业自动化综合实训装置

    3.变频器实训挂箱 配置西门子V20 0.37KW变频器,带有RS485通讯及BOP操作面板. 4.触摸屏实训组件:7英寸昆仑通态,256色,了解工业触摸屏的功能及使用方法.掌握与PLC之间的通信知识 ...

  4. 虎课网计算机二级百度云资源,计算机二级Word篇-实操真题详解20

    本节课我们进行真题演练,让我们开始今天的教程吧. 1.首先是[第一小题]:[CTRL+C复制,CTRL+V粘贴],[然后重命名为Word]. 2.接下来是[第二小题]:打开[文件],进入[页面设置面板 ...

  5. 计算机二级考试word试题及其讲解,计算机二级Word篇-实操真题详解21

    本节课我们进行真题演练,让我们开始今天的教程吧. 1.首先是[第一小题]:[CTRL+C复制,CTRL+V粘贴],[然后重命名为Word]. 2.[第二小题]:双击打开文件,进入[页面设置面板-调整纸 ...

  6. MySQL 大作业实训考试题_2020系统综合实践 期末大作业 15组

    选题简介 这次我们做的是一个人脸对比并返回相似度的网页,部署在Linux的环境下.我们选题的原因是在实验七学习到了一些有关于人脸识别的知识,再加上之前个人实验中学习的知识,包括python.mysql ...

  7. mysql数据库初识实训总结_MySQL数据库初识(基础语句)

    初识Mysql 认知 数据库:DB 所有的数据存放的仓库 每一个文件夹也是数据库 数据库管理员:DBA 管理数据库软件 数据库服务器:一台跑着一个数据库管理软件的机器 表:文件,一张存储了数据的表 数 ...

  8. 计算机二级excel13,计算机二级Excel篇-实操真题详解13

    那就开始今天的教程吧 1.第1题,[Ctrl+C]复制,[Ctrl+V]粘贴,再右键[重命名],改成题目所要求的名字. 2.第2题,双击打开文件,找到涉及金额的单元格,按住[Shift]键往下拖,把右 ...

  9. ❤『面试知识集锦100篇』3.mysql篇丨mysql基础知识和面试真题,看完不收藏算我输!!

    作者:不吃西红柿 简介:CSDN博客专家.蓝桥签约作者.大数据&Python领域优质创作者. 谢谢那些曾经击倒我的人, 躺着可真TM舒服. 目录 一.知识体系 1.关系型数据库术语 2.事务的 ...

最新文章

  1. css动画使用背景图片卡顿_CSS入门学习笔记(二):布局与定位
  2. 关于使用wcf架构分布式系统的一点想法
  3. AI现在能教你画画了
  4. 32岁的老程序员面试没通过,一问原因,挺突然的...
  5. 【C++ 语言】Visual Studio 配置 FFMPEG 开发环境 ( VS2019 CMake 环境安装 | 下载 FFMPEG 开发包 | 配置 FFMPEG )
  6. Codeforces 1314 题解
  7. SpringMVC的工作流程
  8. 生信入门-爱课程上的华中农业大学
  9. 蛋疼的中文编码及其计算机编码历史
  10. jQuery学习笔记系列(三)——事件注册、事件处理、事件对象、拷贝对象、多库共存、jQuery插件、toDoList综合案例
  11. 老男孩IT教育在线3期新学员司毅第一期作业
  12. java加载配置文件
  13. 官宣!CSDN 发布 C 站软件工程师能力认证
  14. mysql 处理一条语句卡死_一条MySQL查询语句,卡死机器,不知道为什么,求高手指点!...
  15. html编码的aacll,高级音频编码(AAC)的一种信息隐藏方法.pdf
  16. 坚持住啊,还在代码屎山中爬行的同事们
  17. 前端-项目引入苹方字体
  18. mongodb的java驱动_MongoDB的java版本驱动
  19. delphi 龙年窗体 恭喜发财
  20. python123测验答案第十周_智慧职教mooc的APPPython程序设计(常州工业职业技术学院)章节测验答案...

热门文章

  1. c语言实例 魔术师的猜牌术(1),C语言实例:魔术师的猜牌术(1)
  2. 51单片机风扇转动c语言代码,基于51单片机的智能风扇控制系统设计.doc
  3. [搜索引擎友好之路]搜索引擎优化常见问题与回答
  4. 雷电模拟器导入burp证书
  5. 【语言-批处理】生成文件时,文件名变化生成(例如:yuan1.txt、yuan2.txt、yuan3.txt...)
  6. 使用变量时,单引号、双引号、反向单引号的区别
  7. ultraedit编辑器快速学习
  8. 【华为云技术分享】如何将90%的代码自动迁移到鲲鹏平台上
  9. 个人永久性免费-Excel催化剂功能第93波-地图数据挖宝之两点距离的路径规划
  10. 环境污染、空气质量数据集:省/市/县PM2.5浓度、空气流通系数、逆温数据