实验目的:
- 掌握连接查询的一般格式。
-
实验内容:
对JXGL数据库进行查询。
查询选修2号课程的学生姓名。
select sname from student,sc
where student.sno=sc.sno and cno='2'
查询钱横同学选修的课程号和分数
select * from student,sc where student.sno=sc.sno and sname like '钱横'
查询CS系学生选修的课程号和分数
查询选修了这样课程的学生学号,其直接先行课是5号课程。
select sno from sc,course where sc.cno=course.cno and cpno='5'
查询选修的课程成绩为90分以上的学生姓名与课程名和成绩。
select sname,cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and grade>90
查询选修2号课程且成绩在90分以上的所有学生的学号、姓名。
select student.sno,sname from student,sc where student.sno=sc.sno and cno='2' and grade>90
查询每个学生的学号、姓名、选修的课程名及成绩。
select student.sno,sname,cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno
查询性别为男、课程成绩及格的学生学号,姓名及课程号、成绩。
select student.*,cno,grade from student,sc where student.sno=sc.sno and grade>=60 and ssex='男'
查询平均成绩大于85分的学号、姓名、平均成绩。
select student.sno,sname,avg(grade) as '平均分' from student,sc where student.sno=sc.sno group by student.sno,sname having avg(grade)>85
查询选修了数据库系统的学生的姓名及成绩。
select sname,grade from student,course,sc where student.sno=sc.sno and course.cno=sc.cno and cname='数据库系统'
查询每个系的学生选修的1号课程的平均成绩,最高分,最低分,选课人数。
select avg(grade) as '平均成绩',max(grade) as '最高分',min(grade) as '最低分',count(*) as '选课人数' from student,sc where student.sno=sc.sno and cno='1' group by sdept
查询同时选修了“1”课程和“2”课程的学生的姓名。(自身连接查询)
select sname from student where sno in (select s1.sno from sc s1,sc s2 where s1.cno='1' and s2.cno='2' and s1.sno=s2.sno)
查询至少选修了2门课程的学生的学生号。(自身连接)
select sname from student where sno in (select sno from sc group by sno having count(*)>=2)
查询每门课程的先修课的先修课。(自身连接)
select c1.cno,c2.cpno from course c1,course c2 where c1.cpno = c2.cno