实验数据如下:
在数据库中建立三个关系表Students,Course,SC。
学生表:Student(Sno,Sname,Ssex,Sage,Sdept)
课程表:Course(Cno,Cname,Cpno,Ccredit)
学生选课表:SC(Sno,Cno,Grade)
在这里插入图片描述在这里插入图片描述
1、 在SC表中查询选修了课程的学生学号。注意去掉重复的行。
2、查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
3、查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
4、查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
5、检索至少选修课程号为C2和C4的学生学号
6、查询每个学生的学号、姓名、选修的课程名及成绩
7、查询总学分超过6分的同学学号,姓名,选修课的门数,总学分
8、查询选修1号课程的男生和女生人数,显示课号,性别,人数。
9、检索学C2课程的学号与姓名。
10、检索平均成绩最高的学生学号。
11、查询没有选修1号课程的学生姓名。
12、查询选修了全部课程的学生姓名。
13、检索刘晨同学不学的课程的课程号。
14、检索学号比刘晨同学大,而年龄比他小的学生姓名。
15、求年龄大于女同学平均年龄的男学生姓名和年龄。

答案:
1.select distinct Sno from sc
2.select Sname ,Ssex from students where Sdept not in ('CS','MA','IS');
3.select * from students order by Sdept,Sage desc;
4.select Sno,Grade from sc where Cno=3 order by Grade desc;
5.select students.Sno from students left outer join sc on(sc.Sno=students.Sno) where sc.Cno='2' and sc.Cno='4'
6.select students.Sno,Sname,Cname,Grade from students,sc,course where students.Sno=sc.Sno and course.Cno=sc.Cno;
7.select Sno,Sname,count(Cno),sum(Ccredit) from( select Students.Sno,Sname,course.Cno,course.Ccredit from studnts,sc,course where students.Sno=sc.Sno and course.Cno=sc.Cno)as sc1 group by Sno having sum(Ccredit)>6
8.select Cno,Ssex,count(Sno) from( select Cno,Ssex,students.Sno from students,sc where students,sc where students.Sno=sc.Sno and Cno=1)as sc1 group by Ssex
9.select students.Sno,Sname from students,sc where students.Sno=sc.Sno and Cno='1'
10.select Sno from sc group by Sno order by avg(Grade) desc limit 1
11.select Sname from students where not exists (select * from sc where students.Sno=sc.Sno and Cno='1')
12.select Sname from students where not exists (select * from course where not exists (select * from sc where Sno=students.Sno and Cno=course.Cno));
13.select Cno from course where not exists (select Cno from sc ahere sc.Cno=course.Cno and Sno='20021522')
14.select Sname from students where Sno>( select Sno from students where Sname='刘晨' and Sage<( select Sage from students where Sname='刘晨'))
15.select Sname,Sage from students where Sage>( select avg(Sage) from students where Ssex='女' ) and Ssex='男'

  • 大小写啥的,复制到数据库编译器里会自动转换,全手写,不知道拼写对不对,你们复制过去注意下。

  • 不知道为什么显示出来没有换行,也没有个格式,不过效果能出来。

  • 这些也都是我自己写的,不知道对不对,反正结果是对的。

  • 完工,吃饭。哈哈

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐