实验目的:

  1. 掌握连接查询的一般格式。
  2. 熟练掌握连接查询的各种方法。

    实验内容:

    对JXGL数据库进行查询。

  3. 查询选修2号课程的学生姓名。

    1. select sname from student,sc
    2. where student.sno=sc.sno and cno='2'
  4. 查询钱横同学选修的课程号和分数

    select * from student,sc 
    where student.sno=sc.sno and sname like '钱横'
    
  5. 查询CS系学生选修的课程号和分数

  6. 查询选修了这样课程的学生学号,其直接先行课是5号课程。

    select sno 
    from sc,course 
    where sc.cno=course.cno and cpno='5'
    
  7. 查询选修的课程成绩为90分以上的学生姓名与课程名和成绩。

    select sname,cname,grade from student,sc,course 
    where student.sno=sc.sno and sc.cno=course.cno and grade>90
    
  8. 查询选修2号课程且成绩在90分以上的所有学生的学号、姓名。

    select student.sno,sname 
    from student,sc 
    where student.sno=sc.sno and cno='2' and grade>90
    
  9. 查询每个学生的学号、姓名、选修的课程名及成绩。

    select student.sno,sname,cname,grade from student,sc,course 
    where student.sno=sc.sno and sc.cno=course.cno
    
  10. 查询性别为男、课程成绩及格的学生学号,姓名及课程号、成绩。

    select student.*,cno,grade from student,sc 
    where student.sno=sc.sno and grade>=60 and ssex='男'
    
  11. 查询平均成绩大于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
    
  12. 查询选修了数据库系统的学生的姓名及成绩。

    select sname,grade 
    from student,course,sc 
    where student.sno=sc.sno and course.cno=sc.cno and cname='数据库系统'
    
  13. 查询每个系的学生选修的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
    
  14. 查询同时选修了“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)
    
  15. 查询至少选修了2门课程的学生的学生号。(自身连接)

    select sname from student where sno in  (select sno from sc 
    group by sno 
    having count(*)>=2)
    
  16. 查询每门课程的先修课的先修课。(自身连接)

    select c1.cno,c2.cpno from course c1,course c2
    where c1.cpno = c2.cno