MySQL数据库,练习表course, sc,student, teacher和测试数据
DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `cno` varchar(11) NOT NULL COMMENT '课程编号', `cname` varchar(255) DEFAULT NULL COMMENT '课程名称', `tno` varchar(11) DEFAULT NULL COMMENT '教师编号', PRIMARY KEY (`cno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `course` VALUES ('001', '软件工程', '001'); INSERT INTO `course` VALUES ('002', '算法分析', '002'); INSERT INTO `course` VALUES ('003', '设计模式', '001'); INSERT INTO `course` VALUES ('004', '大学英语', '003'); INSERT INTO `course` VALUES ('005', '程序设计', '003'); INSERT INTO `course` VALUES ('006', '数学分析', '001');
DROP TABLE IF EXISTS `sc`; CREATE TABLE `sc` ( `sno` varchar(11) NOT NULL COMMENT '学生编号', `cno` varchar(11) NOT NULL COMMENT '课程编号', `score` int(11) DEFAULT NULL COMMENT '成绩', PRIMARY KEY (`cno`,`sno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `sc` VALUES ('001', '001', '80'); INSERT INTO `sc` VALUES ('002', '001', '78'); INSERT INTO `sc` VALUES ('003', '001', '60'); INSERT INTO `sc` VALUES ('006', '001', '59'); INSERT INTO `sc` VALUES ('001', '002', '59'); INSERT INTO `sc` VALUES ('002', '002', '79'); INSERT INTO `sc` VALUES ('003', '002', '56'); INSERT INTO `sc` VALUES ('006', '002', '59'); INSERT INTO `sc` VALUES ('001', '003', '79'); INSERT INTO `sc` VALUES ('002', '003', '80'); INSERT INTO `sc` VALUES ('003', '003', '60'); INSERT INTO `sc` VALUES ('006', '003', '59'); INSERT INTO `sc` VALUES ('002', '004', '66'); INSERT INTO `sc` VALUES ('002', '005', '67'); INSERT INTO `sc` VALUES ('005', '005', '59'); INSERT INTO `sc` VALUES ('004', '001', '66'); INSERT INTO `sc` VALUES ('007', '001', '57'); INSERT INTO `sc` VALUES ('005', '001', '66');
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `sno` varchar(11) NOT NULL COMMENT '学生编号', `sname` varchar(255) DEFAULT NULL COMMENT '学生姓名', `sage` int(11) DEFAULT NULL COMMENT '学生年龄', `ssex` varchar(255) DEFAULT NULL COMMENT '学生性别', PRIMARY KEY (`sno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `student` VALUES ('001', '张三', '22', '男'); INSERT INTO `student` VALUES ('002', '李四', '23', '男'); INSERT INTO `student` VALUES ('003', '晓红', '23', '女'); INSERT INTO `student` VALUES ('004', '小明', '22', '男'); INSERT INTO `student` VALUES ('005', '小黄', '20', '男'); INSERT INTO `student` VALUES ('006', '小王', '24', '男'); INSERT INTO `student` VALUES ('007', '小王', '24', '男');
DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `tno` varchar(11) NOT NULL COMMENT '教师编号', `tname` varchar(255) DEFAULT NULL COMMENT '教师名称', PRIMARY KEY (`tno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `teacher` VALUES ('001', '张老师'); INSERT INTO `teacher` VALUES ('002', '李老师'); INSERT INTO `teacher` VALUES ('003', '黄老师'); INSERT INTO `teacher` VALUES ('004', '红老师'); INSERT INTO `teacher` VALUES ('005', '李晓明'); INSERT INTO `teacher` VALUES ('006', '李老师');
student(sno, sname, sage, ssex)
teacher(tno, tname)
course(cno, cname, tno)
sc(sno, cno, score)
01、查询平均成绩大于60分的同学的学号和平均成绩;(练习having+aggregate函数语句)
select sno,avg(score) from sc group by sno having avg(score)>60;
02、查询所有同学的学号、姓名、选课数、总成绩;(练习where+group by语句)
select sc.sno,sname,count(cno),sum(score) from sc,student where sc.sno=student.sno group by sno;
03、查询所有课程成绩小于60分的同学的学号、姓名;(练习where+group by+having+aggregate函数语句)
select student.sno,sname from sc,student where sc.sno = student.sno group by sc.sno having max(score)<60;
04、查询没有修选所有课程的学生的学号、姓名;
select student.sno,sname from student left join sc on student.sno=sc.sno group by student.sno having count(cno)<>(select count(*) from course);
05、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.sno from sc a,sc b where a.sno=b.sno and a.cno='001' and b.cno='002' and a.score>b.score;06、查询学过“001”并且也学过“002”课程的同学的学号、姓名;
select sno,sname from student where sno in (select a.sno from sc a,sc b where a.sno=b.sno and a.cno='001' and b.cno='002');
07、查询课程“002”的成绩比课程“001”的成绩低的所有同学的学号、姓名;
select sno,sname from student where sno in (select a.sno from sc a,sc b where a.sno=b.sno and a.cno='001' and b.cno='002' and a.score>b.score);
08、查询姓“李”的老师的个数;
select count(tname) from teacher where tname like '李%';
09、查询没学过“黄老师”老师课的同学的学号、姓名;
select sno,sname from student where sno not in (select distinct(sc.sno) from teacher,course,sc where sc.cno=course.cno and teacher.tno = course.tno and teacher.tname="黄老师");10、查询学过“黄老师”老师所教的所有课的同学的学号、姓名;
select sno,sname from student where sno in (select sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and tname='黄老师' group by sno having count(sc.cno)= (select count(cno) from teacher,course where teacher.tno = course.tno and tname='黄老师'));11、查询至少有一门课程与学号为“001”的同学所学相同的同学的学号和姓名;
select distinct student.sno,sname from student,sc where student.sno=sc.sno and student.sno<>'001' and cno in(select cno from sc where sno='001');12、查询和“003”号的同学学习的课程完全相同的其他同学学号和姓名;
select sno from sc where sno<>'003' and sno in (select sno from sc where cno in(select cno from sc where sno='003')) group by sno having count(cno)=(select count(cno) from sc where sno='003');
13、把“sc”表中“黄老师”教的课的成绩都更改为此课程的平均成绩;
update sc a,(select cno,avg(score) score from sc where cno in (select cno from course,teacher where course.tno=teacher.tno and tname='黄老师') group by cno) b set a.score=b.score where a.cno=b.cno;14、删除学习“黄老师”老师课的sc表记录;
delete from sc where cno in (select course.cno from course,teacher where course.tno =teacher.tno and tname='黄老师');15、查询每门课程被选修的学生数
select course.cno,count(sno) from course left join sc on sc.cno=course.cno group by cno;16、查询同名同性学生名单,并统计同名人数
select sname from student group by sname having count(*)>1;17、查询有两门以上不及格课程的同学的学号及其平均成绩
select sno,avg(score) from sc where sno in (select sno from sc where score<60 group by sno having count(*)>2);18、查询全部学生都选修的课程的课程号和课程名
select cno,cname from course where cno in (select cno from sc group by cno having count(sno)=(select count(*) from student));19、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct a.sno,a.cno a_cno,b.cno b_cno,b.score from sc a,sc b where a.score=b.score and a.cno<>b.cno;20、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cno,avg(score) from sc group by cno order by avg(score) asc,cno desc;