mysql考试总结

USE school;
-- 班级表
CREATE TABLE class(cid TINYINT PRIMARY KEY AUTO_INCREMENT,caption VARCHAR(20)
);INSERT INTO class(caption) VALUES("三年二班"),("一年三班"),("三年一班");SELECT * FROM class;-- 老师表
CREATE TABLE teacher(tid TINYINT PRIMARY KEY AUTO_INCREMENT,tname VARCHAR(20)
);INSERT INTO teacher(tname) VALUES("波多"),("苍空"),("饭岛");SELECT * FROM teacher;-- 学生表
CREATE TABLE student(sid TINYINT PRIMARY KEY AUTO_INCREMENT,sname VARCHAR(20),gender VARCHAR(10),class_id TINYINT,FOREIGN KEY (class_id) REFERENCES class(cid)
);INSERT INTO student(sname,gender,class_id) VALUES("钢蛋","女",1),("铁锤","女",1),("山炮","男",2);SELECT * FROM student;-- 课程表
CREATE TABLE course(cid TINYINT PRIMARY KEY AUTO_INCREMENT,cname VARCHAR(20),teacher_id TINYINT,FOREIGN KEY (teacher_id) REFERENCES teacher(tid)
);
ALTER TABLE course MODIFY cid TINYINT, DROP PRIMARY KEY;
ALTER TABLE course ADD CONSTRAINT xx FOREIGN KEY (cid) REFERENCES class(cid);
DESC course;
SHOW CREATE TABLE course;
INSERT INTO course(cname,teacher_id) VALUES("生物",1),("体育",1),("物理",2);SELECT * FROM course;
-- 成绩表
CREATE TABLE score(sid TINYINT PRIMARY KEY AUTO_INCREMENT,student_id TINYINT,course_id TINYINT,number INT,FOREIGN KEY (student_id) REFERENCES student(sid),FOREIGN KEY (course_id) REFERENCES course(cid)
);INSERT INTO score(student_id, course_id, number) VALUES(1,1,60),(1,2,59),(2,2,100);SELECT * FROM score;DELETE FROM score WHERE sid=6;# 二、操作表
#
# 1、自行创建测试数据
#
# 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
SELECT B.student_id FROM
(SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="生物") as AINNER JOIN
(SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="物理") as BON A.student_id=B.student_id AND A.number>B.number;# 3、查询平均成绩大于60分的同学的学号和平均成绩;
INSERT INTO score(student_id, course_id, number) VALUES(2,3,56),(3,1,46),(3,2,59),(3,3,71),(4,1,90),(4,2,27);
SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id HAVING avg(number)>60;
# 4、查询所有同学的学号、姓名、选课数、总成绩;
SELECT student.sid,student.sname,count(student.class_id) 选课数,sum(number) 总成绩FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY sname ORDER BY 总成绩;# 5、查询姓“波”的老师的个数;
SELECT count(*) 波老师个数 FROM teacher WHERE tname LIKE "波%";
INSERT INTO teacher(tname) VALUES ("波大");
# 6、查询没学过“叶平”老师课的同学的学号、姓名;-- 得到所有同学学过的课程及其对应的老师,然后对应筛选
INSERT INTO teacher(tname) VALUES("叶平");
INSERT INTO course(cname, teacher_id) VALUES("历史",5);
INSERT INTO score(student_id, course_id, number) VALUES(1,5,81);SELECT student.sid,student.sname,A.tname FROM scoreINNER JOIN student ON score.student_id = student.sidINNER JOIN (SELECT DISTINCT course.cid,course.teacher_id,course.cname,teacher.tname FROMcourse INNER JOIN teacher ON course.teacher_id = teacher.tid) AS A ON score.course_id = A.cidGROUP BY A.tname HAVING A.tname!="叶平";# 7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
INSERT INTO student(sname,gender,class_id) VALUES("张三","男",3);
INSERT INTO score(student_id, course_id, number) VALUES(5,2,63);SELECT B.sid 学号,B.sname 姓名 FROM(SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id=1) AS AINNER JOIN(SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id=2) AS B ON A.sid = B.sid;# 8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT * FROM student;    SELECT * FROM score;
INSERT INTO student(sname, gender, class_id) VALUES("王五","男",3);
INSERT INTO score(student_id, course_id, number) VALUES(8,5,93);SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN studentON score.student_id = student.sid AND course_id=5;# 9、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
INSERT INTO student(sname, gender, class_id) VALUES("赵六","女",2);
INSERT INTO score(student_id, course_id, number) VALUES(9,2,65),(9,1,70);
UPDATE score SET number = 65 WHERE sid=21;SELECT * FROM student WHERE(SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=2)<(SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=1);# 10、查询有课程成绩小于60分的同学的学号、姓名;
SELECT student.sid,student.sname FROM studentINNER JOIN score ON student.sid = score.student_id WHERE score.number<60 GROUP BY student.sname;
INSERT INTO student(sname, gender, class_id) VALUES("钢镚","男",2);
INSERT INTO score(student_id, course_id, number) VALUES(4,3,48);
# 11、查询没有学全所有课的同学的学号、姓名;
-- 测试
SELECT count(cid) 总课程数 FROM course;
SELECT count(course_id) study_course FROM score GROUP BY student_id;
SELECT * FROM (SELECT count(cid) 总课程数 FROM course) AS AINNER JOIN(SELECT count(course_id) study_course FROM score GROUP BY student_id) AS BON A.总课程数 = B.study_course;-- 正确答案
SELECT student.sid,student.sname,count(course_id) 学习课程数 FROM score INNER JOIN studentON score.student_id = student.sid GROUP BY student_idHAVING count(course_id)=(SELECT count(cid) FROM course);# 12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
#
# 13、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
#
# 14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;# 15、删除学习“叶平”老师课的SC表记录;
#
# 16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
#
# 17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
#
# 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT course_id,max(number) 最高分,min(number) 最低分 FROM score GROUP BY course_id;INSERT INTO score(student_id, course_id, number) VALUES(2,1,76);
# 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;-- 分析求出平均成绩并计算及格率
SELECT avg(number) 课程平均成绩 FROM score GROUP BY course_id;
SELECT count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id;
SELECT count(student_id) 各科总人数 FROM score GROUP BY course_id;-- 答案如下
SELECT A.课程平均成绩,B.各科不及格人数/C.各科总人数 AS 及格率 FROM(SELECT course_id,avg(number) 课程平均成绩 FROM score GROUP BY course_id) AS AINNER JOIN(SELECT course_id,count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id) AS BINNER JOIN(SELECT course_id,count(student_id) 各科总人数 FROM score GROUP BY course_id) AS CON A.course_id = B.course_id AND A.course_id = C.course_id ORDER BY A.课程平均成绩;# 20、课程平均分从高到低显示(显示任课老师);
SELECT teacher.tname,course.cname FROM teacher,course WHERE course.teacher_id = teacher.tid;
SELECT avg(number) 课程平均分 FROM teacher,score GROUP BY course_id;
-- 答案如下
SELECT A.tname 任课老师,B.课程平均分 FROM(SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS AINNER JOIN(SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS BON A.cid=B.course_id ORDER BY B.课程平均分 DESC;# 21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT * FROM score ORDER BY course_id;# 22、查询每门课程被选修的学生数;-- 分析  根据成绩根据课程进行分类,按照学生ID统计次数
SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id;# 23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT student.sid,student.sname FROM(SELECT * FROM score GROUP BY student_id HAVING count(student_id)=1) AS AINNER JOIN student ON A.student_id = student.sid;
# 24、查询男生、女生的人数;
SELECT boy.男,girl.女 FROM(SELECT count(gender) 男 FROM student WHERE gender="男") as boy,(SELECT count(gender) 女 FROM student WHERE gender="女") as girl;# 25、查询姓“张”的学生名单;
SELECT sid,sname FROM student WHERE sname LIKE "张%";
# 26、查询同名同姓学生名单,并统计同名人数;
SELECT sname 姓名,count(sname) 同名人数 FROM student GROUP BY sname;
# 27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT A.cid 课程号,B.课程平均分 FROM(SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS AINNER JOIN(SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS BON A.cid=B.course_id ORDER BY B.课程平均分 DESC;# 28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT student.sid 学号,student.sname 姓名,avg(score.number) 平均成绩 FROMstudent INNER JOIN score ON student.sid = score.student_id GROUP BY sname;# 29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;
SELECT course.cname 课程,student.sname 姓名,score.number 分数 FROM score INNER JOIN student INNER JOIN courseON score.student_id = student.sid AND score.course_id=course.cidWHERE course.cname="生物" AND score.number<60;# 30、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN studentON score.student_id = student.sid AND score.course_id=3 AND score.number>80;# 31、求选了课程的学生人数
INSERT INTO student(sname, gender, class_id) VALUES("李四","男",3);
INSERT INTO student(sname, gender, class_id) VALUES("胜七","女",2);
INSERT INTO score(student_id, course_id, number) VALUES(7,1,74);
-- 答案如下
SELECT count(A.student_id) 选课人数 FROM (SELECT DISTINCT student_id FROM score) AS A;# 32、查询选修“苍空”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
SELECT student.sname 姓名,max(number) 成绩 FROM score INNER JOIN student INNER JOIN course INNER JOIN teacherON score.student_id=student.sid AND score.course_id=course.cid AND course.teacher_id=teacher.tidWHERE teacher.tname="苍空";# 33、查询各个课程及相应的选修人数;
SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id;
# 34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
#
# 35、查询每门课程成绩最好的前两名;
#
# 36、检索至少选修两门课程的学生学号;-- 分析 根据学生ID进行分组,统计课程出现次数,筛选课程次数大于等于2的学生
SELECT student_id FROM score GROUP BY student_id HAVING count(course_id)>=2;
# 37、查询全部学生都选修的课程的课程号和课程名;
SELECT score.student_id 学生ID,course.cname 所选课程,course.cid 课程ID FROM score INNER JOIN courseON score.course_id = course.cid ORDER BY 学生ID;# 38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
#
# 39、查询两门以上不及格课程的同学的学号及其平均成绩;
SELECT A.student_id 学号,B.平均成绩 FROM(SELECT student_id FROM score WHERE number<60 GROUP BY student_id HAVING count(number)>=2) AS A
INNER JOIN(SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id) AS B ON A.student_id=B.student_id;# 40、检索“1”课程分数小于60,按分数降序排列的同学学号;
SELECT student_id FROM score WHERE course_id=1 AND number<60;
# 41、删除“2”同学的“1”课程的成绩;-- 此题已答

  

转载于:https://www.cnblogs.com/nulige/p/6544052.html

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

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

相关文章

反思

1.说明一下ArrayList和数组的区别&#xff0c;并且分别写出初始化的语句&#xff1a; ArrayList:可以放不同的类型&#xff0c;长度不固定 数组&#xff1a;放同一类型&#xff0c;长度固定 数组的初始化语句&#xff1a;int []anew int []{}; ArrayList初始化语句&#xff1a;…

HALCON示例程序high.hdev使用不同方法提取区域

HALCON示例程序high.hdev使用不同方法提取区域 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 dev_close_window () read_image (Mreut, ‘mreut_y’) get_image_size (Mreut, Width, Height) dev_open_window (0, 0, Width, Height, ‘black’, WindowHan…

阅读好书依然是提升自己的高效方法:兼以作者的身份告诉大家如何选择书,以及高效学习的方法...

国内技术网站多如牛毛&#xff0c;质量高的网站也不少&#xff0c;博客园也算一个&#xff0c;各类文章数以百万计&#xff0c;我随便输入一个关键字&#xff0c;比如Spring Cloud&#xff0c;都能看到大量的技术文章和教学视频&#xff0c;我无意贬低技术文章和教学视频的作用…

TCP/IP 协议簇的逐层封装

在使用 TCP 协议的网络程序中&#xff0c;用户数据从产生到从网卡发出去一般要经过如下的逐层封装过程&#xff1a; 从下往上看&#xff1a; 1&#xff09;链路层通过加固定长度的首部、尾部来封装 IP 数据报(Datagram) 产生以太网帧(Frame)。 其中首部存在对封装数据的…

【开源程序(C++)】获取bing图片并自动设置为电脑桌面背景

众所周知&#xff0c;bing搜索网站首页每日会更新一张图片&#xff0c;张张漂亮&#xff08;额&#xff0c;也有一些不合我口味的&#xff09;&#xff0c;特别适合用来做电脑壁纸。 我们想要将bing网站背景图片设置为电脑桌面背景的通常做法是&#xff1a; 上网&#xff0c;搜…

UIProgressView 圆角

里面外面都变成圆角 不用图片 直接改变layer 重点是里面外面都是圆角哦 for (UIImageView * imageview in self.progress.subviews) { imageview.layer.cornerRadius 5; imageview.clipsToBounds YES; } 转载于:https://www.cnblogs.com/huoran1120/p/5563991.html

HALCON示例程序holes.hdev孔洞提取

HALCON示例程序holes.hdev孔洞提取 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 read_image (Image, ‘progres’) get_image_size (Image, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, ‘white’, WindowID) dev_set_co…

给实例动态增加方法VS给类动态增加方法

一、给实例绑定方法 object.method MethodType(method,object) >>>class Badbrains(): pass >>>def mocking(self): print(Brain\s Mocking) >>>b Badbrains() >>>from types import MethodType >>>b.mocking MethodType(moc…

一句DOS命令搞定文件合并

用Dos的copy命令实现&#xff1a; copy a.jsb.jsc.js abc.js /b 将 a.js b.js c.js 合并为一个 abc.js&#xff0c;最后的 /b 表示文件为二进位文件&#xff0c;copy 命令的其它参数可以在 cmd 里输入 copy /? 学习 举例&#xff1a;如果想要合并多个js文件到某个目录下&#…

DataTables warning: Requested unknown parameter '0' from the data source for row '0'

问题&#xff1a;DataTables warning: Requested unknown parameter 0 from the data source for row 0 代码&#xff1a; <script type"text/javascript">var data [{"Name":"UpdateBootProfile","Result":"PASS",&…

HALCON示例程序hull.hdev区域提取与凸度筛选

HALCON示例程序hull.hdev区域提取与凸度筛选 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 read_image (Hull, ‘hull’) get_image_size (Hull, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, ‘black’, WindowID) dev_di…

我与Linux系统的交集

2019独角兽企业重金招聘Python工程师标准>>> 一、初识Linux 第一次知道Linux还是在我刚进大学的时候&#xff0c;从开始聊QQ、玩斗地主的时候起我就是用的Windows&#xff0c;从Windows2000一直到Windows7&#xff0c;当时我已经完全习惯了使用Windows&#xff0c;而…

squid白名单

http_access deny all #取消注释 http_access allow all --> http_access allow xxx_custom_ip#添加系统服务器IP白名单 acl xdaili_custom_ip src 60.191.4.xxx/32 acl xdaili_custom_ip src 139.196.210.xxx/32 acl xdaili_custom_ip src 139.196.172.xxx/32 acl xdail…

HALCON示例程序IC.hdev通过电路板元器件定位识别

HALCON示例程序IC.hdev通过电路板元器件定位识别 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 dev_close_window () read_image (Image, ‘ic’) get_image_size (Image, Width, Height) dev_open_window (0, 0, Width, Height, ‘black’, WindowID) de…

IP头、TCP头、UDP头详解以及定义

一、MAC帧头定义 /*数据帧定义&#xff0c;头14个字节&#xff0c;尾4个字节*/ typedef struct _MAC_FRAME_HEADER { char m_cDstMacAddress[6]; //目的mac地址 char m_cSrcMacAddress[6]; //源mac地址 short m_cType;      //上一层协议类型&#xff0c;如…

关于iOS 3D touch 指纹验证的随笔

file:///Users/OWen/Desktop/3DTouch.png 随着iOS系统不断的更新迭代&#xff0c;苹果总会推出一些新的功能&#xff0c;今天就研究了一下iOS8之后推出的指纹验证的功能&#xff0c;然后写了一个小demo&#xff0c;与君分享。。 上demo&#xff1a; 1&#xff1a;首先导入一个…

HALCON示例程序inner_rectangle1.hdev木板有效区域提取

HALCON示例程序inner_rectangle1.hdev木板有效区域提取 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 dev_update_window (‘off’) dev_update_var (‘off’) dev_update_pc (‘off’) read_image (Image, ‘wood_knots’) get_image_size (Image, Width…

vsCode ext install 不工作

vsCode 扩展商店 不可用 ext 不可用 ext install eamodio.gitlens 1、搜索离线插件https://marketplace.visualstudio.com/vscode 2、插件离线插件模板https://marketplace.gallery.vsassets.io/_apis/public/gallery/publisher/marketplace/extension/${extension name}/${ver…

递归实现 十进制转换其他进制(2-16)

1 def to_str(n, base): 2 convert_string "0123456789ABCDEF" 3 if n < base: 4 return convert_string[n] 5 else: 6 return to_str(n / base, base) convert_string[n % base] 7 8 print to_str(168, 8) 转载于:https://www.cn…

在UltraEdit的查找和替换中使用正则表达式 (转)

很多朋友都用过或者正在用UltraEdit&#xff0c;这个编辑器陪伴我也好几年了&#xff0c;从很多地方影响着我写代码的快捷键习惯&#xff0c;Ultraedit提供了非常丰富的编辑功能&#xff0c;其中非常重要的查找和替换功能一定大家都用过&#xff0c;Ultraedit提供的查找替换功能…