#实训课结课作业:demo 演示(以下内容演示基于MySQL5.7版本!
#1.先创建数据库
CREATE DATABASE demo CHARACTER SET 'utf8';
USE demo;
SHOW CREATE DATABASE demo;#查看数据库结构
#2.创建表 以下创建表未采用外键约束(不推荐外键约束)
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
)
INSERT INTO `class` VALUES
('1', '三年二班'),
('2', '三年三班'),
('3', '一年二班'),
('4', '二年九班');
SELECT * FROM `class`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`)
)
INSERT INTO `course` VALUES
('1', '生物', '1'),
('2', '物理', '2'),
('3', '体育', '3'),
('4', '美术', '2');
insert into `course`
values('5','语文','4'),
('6','数学','4'),
('7','英语','5');
SELECT * FROM `course`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`)
)
DESC score;
INSERT INTO `score`(student_id,course_id ,num)VALUES
( '3', '5', '66'),
( '3', '6', '87'),
( '3', '7', '99'),
( '4', '5', '11'),
( '4', '6', '67'),
( '4', '7', '100'),
( '5', '5', '11'),
( '5', '6', '67'),
( '5', '7', '100'),
( '6', '5', '100'),
( '6', '6', '67'),
( '6', '7', '100'),
( '7', '5', '100'),
( '7', '6', '67'),
( '7', '7', '88'),
( '8', '5', '100'),
('8', '6', '67'),
('8', '7', '88'),
( '9', '5', '88'),
( '9', '6', '67'),
('9', '7', '22'),
( '10', '5', '77'),
( '10', '6', '43'),
( '10', '7', '87'),
( '11', '5', '77'),
( '11', '6', '43'),
( '11', '7', '87'),
( '12', '5', '77'),
( '12', '6', '43'),
( '12', '7', '87');
INSERT INTO `score`(student_id,course_id ,num)VALUES
( '13', '6', '87'),
('13', '7', '75');
INSERT INTO `score` VALUES
('1', '1', '1', '10'),
('2', '1', '2', '9'),
('5', '1', '4', '66'),
('6', '2', '1', '8'),
('8', '2', '3', '68'),
('9', '2', '4', '99'),
('10', '3', '1', '77'),
('11', '3', '2', '66'),
('12', '3', '3', '87'),
('13', '3', '4', '99'),
('14', '4', '1', '79'),
('15', '4', '2', '11'),
('16', '4', '3', '67'),
('17', '4', '4', '100'),
('18', '5', '1', '79'),
('19', '5', '2', '11'),
('20', '5', '3', '67'),
('21', '5', '4', '100'),
('22', '6', '1', '9'),
('23', '6', '2', '100'),
('24', '6', '3', '67'),
('25', '6', '4', '100'),
('26', '7', '1', '9'),
('27', '7', '2', '100'),
('28', '7', '3', '67'),
('29', '7', '4', '88'),
('30', '8', '1', '9'),
('31', '8', '2', '100'),
('32', '8', '3', '67'),
('33', '8', '4', '88'),
('34', '9', '1', '91'),
('35', '9', '2', '88'),
('36', '9', '3', '67'),
('37', '9', '4', '22'),
('38', '10', '1', '90'),
('39', '10', '2', '77'),
('40', '10', '3', '43'),
('41', '10', '4', '87'),
('42', '11', '1', '90'),
('43', '11', '2', '77'),
('44', '11', '3', '43'),
('45', '11', '4', '87'),
('46', '12', '1', '90'),
('47', '12', '2', '77'),
('48', '12', '3', '43'),
('49', '12', '4', '87'),
('52', '13', '3', '87');
SELECT * FROM `score`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`)
)
INSERT INTO `student`
VALUES ('1', '男', '1', '理解'),
('2', '女', '1', '钢蛋'),
('3', '男', '1', '张三'),
('4', '男', '1', '张一'),
('5', '女', '1', '张二'),
('6', '男', '1', '张四'),
('7', '女', '2', '铁锤'),
('8', '男', '2', '李三'),
('9', '男', '2', '李一'),
('10', '女', '2', '李二'),
('11', '男', '2', '李四'),
('12', '女', '3', '如花'),
('13', '男', '3', '刘三'),
('14', '男', '3', '刘一'),
('15', '女', '3', '刘二'),
('16', '男', '3', '刘四');
SELECT * FROM `student`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
)
INSERT INTO `teacher`
VALUES ('1', '张磊老师'),
('2', '李平老师'),
('3', '刘海燕老师'),
('4', '朱云海老师'),
('5', '李杰老师');
SELECT * FROM `teacher`;
#1、自行创建测试数据
DELIMITER $ #-第一项数据测试学生表的全体数据
CREATE PROCEDURE select_all_data()
BEGIN
SELECT * FROM `student`;
END $
DELIMITER ;
#数据调用
CALL select_all_data();
SELECT * FROM `student`;
#第二项数据测试 in/out测试具体的人所选着的课程号
DELIMITER $
CREATE PROCEDURE select_all_student(IN `name` VARCHAR(32),OUT class INT(11))
BEGIN
SELECT class_id INTO class
FROM `student`
WHERE sname = `name`;
END $
DELIMITER ;
#DROP PROCEDURE select_all_student;
#调用
SET @`name` = '张三';
CALL select_all_student(@`name`,@class);
SELECT @class;
##############
DESC `student`;
SELECT * FROM `student`;
#2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
#方法一:
SELECT student_id,num
FROM course c LEFT JOIN score s
ON c.cid = s.course_id
WHERE cname = '生物'
AND num > ANY(
SELECT num
FROM course c LEFT JOIN score s
ON c.cid = s.course_id
WHERE cname = '物理'
);
#方法二
SELECT c.student_id,c.num
FROM (SELECT num,student_id FROM course c LEFT JOIN score s
ON c.cid = s.course_id
WHERE cname = '生物') c
LEFT JOIN (SELECT num,student_id FROM course c LEFT JOIN score s
ON c.cid = s.course_id
WHERE cname = '物理') d
ON c.student_id = d.student_id
WHERE c.num > d.num;
#3、查询平均成绩大于60分的同学的学号和平均成绩;
#方法一:加姓名的连接
SELECT c.student_id,s.sname,avg(num)
FROM student s RIGHT JOIN score c
ON s.sid = c.student_id
GROUP BY student_id
HAVING AVG(num) >60;
#方法二:不加姓名
SELECT student_id,AVG(num)
FROM score
GROUP BY student_id
HAVING AVG(num) > 60;
#老师例子:
SELECT B.student_id,student.sname,B.s_num
FROM (SELECT student_id,avg(num) AS s_num from score GROUP BY student_id HAVING s_num>60)
as B LEFT JOIN student on B.student_id = student.sid;
SELECT student_id,num
FROM score
#4、查询所有同学的学号、姓名、选课数、总成绩;
SELECT DISTINCT s.student_id,c.sname,COUNT(course_id)"选课数",SUM(num)"总成绩"
FROM score s LEFT JOIN student c
ON s.student_id = c.sid
GROUP BY c.sname #此题分组中不要用聚合函数里面的属性去分组
ORDER BY 总成绩 DESC;
#5、查询姓“李”的老师的个数;
SELECT COUNT(*)
FROM teacher
WHERE tname LIKE '%李%';
#6、查询没学过“李平”老师课的同学的学号、姓名;
SELECT sid,sname
FROM student
WHERE sid NOT in(
SELECT e.student_id
FROM score e LEFT JOIN course c
ON e.course_id = c.cid
WHERE e.course_id in(
SELECT c.cid
FROM course c LEFT JOIN teacher t
ON c.teacher_id = t.tid
WHERE tname LIKE '%李平%' #为什么不能用NOT LIKE?
)
GROUP BY e.student_id
);
#例子
SELECT student.sid,student.sname
FROM student
WHERE student.sid NOT IN (
SELECT score.student_id
FROM score LEFT JOIN course
ON score.course_id = course.cid
WHERE score.course_id IN (
SELECT course.cid
FROM course LEFT JOIN teacher
ON course.teacher_id = teacher.tid
WHERE teacher.tname LIKE "李平%"
)
GROUP BY score.student_id)
#7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT d.sid,d.sname
FROM score s LEFT JOIN student d
ON s.student_id = d.sid
WHERE s.course_id = 1 OR s.course_id = 2 #WHERE s.course_id BETWEEN 1 AND 2 #BETWEEN...AND 是一个闭区间 #WHERE s.course_id in(1,2)
GROUP BY d.sid HAVING COUNT(*)=2;
#例子
SELECT B.sid,B.sname
FROM(
SELECT student.sid,score.course_id,student.sname
FROM score LEFT JOIN student
ON score.student_id = student.sid
WHERE score.course_id BETWEEN 1 AND 2
) AS B
GROUP BY B.sid HAVING COUNT(B.course_id)=2;
#8、查询学过“李平”老师所教的所有课的同学的学号、姓名;
SELECT sid,sname
FROM student
WHERE sid in(
SELECT e.student_id
FROM score e LEFT JOIN course c
ON e.course_id = c.cid
WHERE e.course_id in(
SELECT c.cid
FROM course c LEFT JOIN teacher t
ON c.teacher_id = t.tid
WHERE tname LIKE '%李平%'
)
GROUP BY e.student_id HAVING COUNT(*)=2
);
SELECT student.sid,student.sname
FROM score LEFT JOIN student ON score.student_id=student.sid
WHERE score.course_id in
(SELECT course.cid
FROM course LEFT JOIN teacher
on course.teacher_id=teacher.tid
WHERE tname LIKE "%李平%")
GROUP BY student.sid HAVING COUNT(student_id)=2;
#9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT A.sid,A.sname,A.num as score_2,B.num as score_1
FROM
(SELECT student.sid,student.sname,score.num
FROM score LEFT JOIN student on score.student_id=student.sid
WHERE score.course_id=2) AS A
JOIN
(SELECT student.sid,student.sname,score.num
FROM score LEFT JOIN student on score.student_id=student.sid
WHERE score.course_id=1) AS B
ON A.sid = B.sid WHERE A.num<B.num;
#10、查询有课程成绩小于60分的同学的学号、姓名;
SELECT student.sid,student.sname
FROM student
WHERE sid IN
(SELECT score.student_id
FROM score WHERE score.num<60
GROUP BY student_id);
#11、查询没有学全所有课的同学的学号、姓名;
SELECT student.sid,student.sname
FROM student
WHERE sid in
(SELECT student_id
FROM score GROUP BY student_id
HAVING count(course_id)!=
(SELECT COUNT(cid) FROM course));
#12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
SELECT student.sid,student.sname
FROM student
where student.sid in
(SELECT score.student_id
FROM score WHERE course_id in
(SELECT score.course_id
FROM score WHERE student_id=1)
and student_id !=1 GROUP BY student_id);
#13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
SELECT student.sid,student.sname
FROM student
where student.sid in
(SELECT score.student_id
FROM score WHERE course_id in
(SELECT score.course_id FROM score WHERE student_id=1)
and student_id !=1 GROUP BY student_id
having count(1) =(SELECT count(score.course_id) FROM score WHERE student_id=1));
#14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT sid AS 学号,sname AS 姓名
FROM student WHERE sid IN(
SELECT student_id FROM score
WHERE student_id IN
( SELECT student_id FROM score
WHERE student_id!=2 GROUP BY student_id
HAVING COUNT(1)=(SELECT COUNT(1)
FROM score WHERE student_id=2))
AND course_id in (SELECT course_id FROM score WHERE student_id=2)
GROUP BY student_id HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE student_id=2))
#15、删除学习“叶平”老师课的SC表记录;
DELETE FROM score WHERE course_id IN
(SELECT cid FROM course
LEFT JOIN teacher on course.teacher_id=teacher.tid
WHERE tname="叶平老师")
#16、向SC表中插入一些记录,这些记录要求符合以下条件:
①没有上过编号“002”课程的同学学号;
②插入“002”号课程的平均成绩;
INSERT into score(student_id,course_id,num)
SELECT student_id,2,(SELECT AVG(num) FROM score WHERE course_id=2) AS "平均成绩"
FROM score
WHERE course_id!=2
group by student_id
SELECT AVG(num) "平均成绩" FROM score WHERE course_id=2
#17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,
按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
SELECT student_id AS 学生ID,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="语文" AND student_id=s1.student_id) AS 语文,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="数学" AND student_id=s1.student_id) AS 数学,
(SELECT num FROM score LEFT JOIN course on score.course_id=course.cid WHERE course.cname="英语" AND student_id=s1.student_id) AS 英语
FROM score as s1 GROUP BY student_id;
#18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT score.course_id "课程ID",course.cname "课程名",MAX(num) "最高分",MIN(num) "最低分"
FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id;
#19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
#平均分:
SELECT score.course_id AS 课程ID,course.cname as 课程名,AVG(num) AS 平均分 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id ORDER BY 平均分 ASC;
#及格率:
SELECT A.cid,A.cname,ROUND(B.s/A.ss*100,1) AS 及格率
FROM(SELECT cid,cname,COUNT(course_id) AS ss
FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id) as A
INNER JOIN
(SELECT course_id,COUNT(num) as s
FROM score WHERE score.num>60 GROUP BY course_id) as B
ON A.cid=B.course_id ORDER BY 及格率 DESC;
#整合:
SELECT x.课程ID,x.课程名,x.平均分,xx.及格率 FROM
(SELECT score.course_id AS 课程ID,course.cname as 课程名,AVG(num) AS 平均分 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id ORDER BY 平均分 ASC) AS x
LEFT JOIN
(SELECT A.cid,A.cname,ROUND(B.s/A.ss*100,1) AS 及格率 FROM
(SELECT cid,cname,COUNT(course_id) AS ss FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id) as A
INNER JOIN
(SELECT course_id,COUNT(num) as s FROM score WHERE score.num>60 GROUP BY course_id) as B ON A.cid=B.course_id ORDER BY 及格率 DESC) AS xx
ON x.课程ID=xx.cid
#标准
SELECT course_id,AVG(num) AS 平均分,ROUND(sum(CASE WHEN num<60 then 0 ELSE 1 END)/sum(1)*100,1) AS 及格率
FROM score GROUP BY course_id ORDER BY 平均分 ASC,及格率 DESC;
#20、课程平均分从高到低显示(现实任课老师);
SELECT course.cname,teacher.tname,xxx.平均分 FROM course
LEFT JOIN teacher ON course.teacher_id=teacher.tid
LEFT JOIN (SELECT course_id,AVG(num) AS 平均分 FROM score GROUP BY course_id) AS xxx ON xxx.course_id=course.cid
ORDER BY xxx.平均分 DESC
#21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT s1.cid AS 课程ID,s1.cname AS 课程,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 0,1) AS 第一,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 1,1) AS 第二,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 2,1) AS 第三
FROM course AS s1
#22、查询每门课程被选修的学生数;
SELECT course.cid AS 课程ID,course.cname AS 课程名,COUNT(1) AS 学生数
FROM score LEFT JOIN course ON score.course_id=course.cid
GROUP BY score.course_id;
#23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT student.sid AS 学号,student.sname AS 姓名 FROM student
WHERE sid IN
(SELECT student_id FROM score GROUP BY student_id HAVING count(*))
#24、查询男生、女生的人数;
SELECT gender AS 性别,COUNT(1) AS 人数 FROM student GROUP BY gender;
#25、查询姓“张”的学生名单;
SELECT * FROM student WHERE sname LIKE "张%"
#26、查询同名同姓学生名单,并统计同名人数;
SELECT sname As 姓名,COUNT(1) AS 人数 FROM student GROUP BY sname;
#27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT course.cid AS 课程ID,course.cname AS 课程名,AVG(IF(ISNULL(num),0,score.num)) AS 平均分
FROM score LEFT JOIN course ON score.course_id = course.cid
GROUP BY score.course_id ORDER BY 平均分 ASC,课程ID DESC;
#28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT course.cid AS 课程ID,course.cname AS 课程名,AVG(IF(ISNULL(num),0,score.num)) AS 平均分
FROM score LEFT JOIN course ON score.course_id = course.cid
GROUP BY score.course_id HAVING 平均分>85;
#29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
SELECT student.sid AS 学号,student.sname AS 姓名,score.num AS 成绩 FROM score
LEFT JOIN course ON score.course_id=course.cid
LEFT JOIN student ON score.student_id= student.sid
WHERE course.cname="数学" AND score.num<60;
#30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
SELECT student.sid AS 学号,student.sname AS 姓名 FROM student WHERE sid IN
(SELECT student_id FROM score WHERE score.course_id =3 AND num > 80)
#31、求选了课程的学生人数
SELECT COUNT(A.student_id) AS "总人数"
FROM (SELECT student_id FROM score GROUP BY student_id) AS A
SELECT COUNT(DISTINCT student_id) AS "总人数" FROM score;
#32、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
SELECT student.sid AS 学号,student.sname AS 姓名,num AS 成绩 FROM score
LEFT JOIN course ON score.course_id=course.cid
LEFT JOIN student ON score.student_id=student.sid
LEFT JOIN teacher ON course.teacher_id=teacher.tid
WHERE teacher.tname = "张磊老师" ORDER BY num DESC LIMIT 1;
#33、查询各个课程及相应的选修人数;
SELECT course_id AS ID,course.cname AS 课程,count(1) AS 人数 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id
#34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
SELECT DISTINCT s1.student_id,s1.course_id,s1.num
FROM score AS s1,score AS s2
WHERE s1.student_id != s2.student_id
AND s1.course_id!=s2.course_id AND s1.num=s2.num
#35、查询每门课程成绩最好的前两名;
SELECT cid AS 课程ID,cname AS 课程,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 0,1) AS 第一,
(SELECT num FROM score AS s2 WHERE s2.course_id=s1.cid GROUP BY num ORDER BY num DESC LIMIT 1,1) AS 第二
FROM course AS s1
#36、检索至少选修两门课程的学生学号;
SELECT student_id
FROM score
GROUP BY student_id
HAVING count(course_id)>=2
#37、查询全部学生都选修的课程的课程号和课程名;
SELECT course.cid AS 课程号,course.cname AS 课程名 FROM score
LEFT JOIN course ON score.course_id=course.cid GROUP BY score.course_id
HAVING COUNT(student_id)=(SELECT COUNT(*) FROM student)
#38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
SELECT student.sid,student.sname FROM student
WHERE student.sid not IN (SELECT student_id FROM score
WHERE course_id IN(SELECT cid FROM course
LEFT JOIN teacher ON course.teacher_id=teacher.tid
WHERE teacher.tname="李平老师" ) GROUP BY student_id)
#39、查询两门以上不及格课程的同学的学号及其平均成绩;
SELECT student_id AS 学号,sname AS 姓名,AVG(num) AS 平均成绩 FROM score
LEFT JOIN student ON score.student_id=student.sid
WHERE student_id IN (SELECT student_id FROM score
WHERE num<60 GROUP BY student_id HAVING COUNT(1)>=2)
GROUP BY student_id
#40、检索“004”课程分数小于60,按分数降序排列的同学学号;
SELECT student_id,num FROM score
WHERE score.course_id=4 AND num <60
ORDER BY num ASC
#41、删除“002”同学的“001”课程的成绩;
delete from score
where student_id=2
and course_id=1;
#查询删除结果
SELECT *
from score
where student_id=2
and course_id=1;
MySQL实训结课作业练习(41道题)
最新推荐文章于 2024-01-11 14:12:48 发布