实验三-多表查询

1. (简答题) 查询学号为201715121的学生的所选的全部课程的课程名和成绩。


(1)select cname,score

          from stc,courses

          where stc.cno=courses.cno

          and sc.sno='201715121'

(2)select cname,score

           from  stc NATURAL JOIN courses

           where sc.sno='201715121'

(3)select cname,score

         from  stc JOIN courses on sc.cno=courses.cno

        where sc.sno='201715121'

2. (简答题) 查询选修计算机导论课程的学生学号和姓名。(要求用where连接实现)

select students.sno,sname

from students,stc,courses

where students.sno=stc.sno 

   and stc.cno=courses.cno

   and cname='计算机导论';

3. (简答题)查询选修计算机导论课程的学生学号和姓名(要求用JOIN…ON连接实现)

select students.sno,sname

 from students NATURAL join stc 

 NATURAL join courses

 where cname='计算机导论';

或者:

select students.sno,sname

 from students join stc on students.sno=stc.sno

 join courses on stc.cno=courses.cno

 and cname='计算机导论';

4. (简答题)查询选修计算机导论课程的学生学号和姓名(要求用嵌套查询实现)

select students.sno,sname

from students

where sno in (

  select sno from stc

  where cno in (

     select cno from courses

      where cname='计算机导论' ) );

或者:

select students.sno,sname

from students

where exists (

  select * from stc,courses

  where stc.cno=courses.cno 

    and students.sno=stc.sno 

    and cname='计算机导论') ;

5. (简答题) 查询没有选修任何课程的学生信息。

(1)用外连接实现

select students.*

from students left join stc on students.sno=stc.sno 

where stc.sno is null;



(2)用嵌套查询实现

select students.*

from students 

where sno not in (

   select distinct sno 

   from stc);

6. (简答题) 查询至少被三名学生选修的课程号和课程名称。

提示:课程名可能不唯一,但课程号唯一。

select courses.cno,cname,count(*)

from stc join courses on stc.cno=courses.cno

group by courses.cno having count(*)>=3;   



group by子句也可写成:

group by courses.cno,cname  having count(*)>=3;   

7. (简答题)查询同时选修了B001和B002号课程的学生学号。

select distinct sno from stc

where  cno='b001'  

and sno in (

   select sno from stc 

   where  cno='b002' );

8. (简答题) 查询同时选修了高等数学和C程序设计课程的学生学号。

select distinct sno

from stc

where cno in ( select cno from courses where cname='计算机导论' ) 

  and sno in ( select sno from stc  where cno in (

                             select cno from courses  where cname='高等数学' )

                    ) ;

9. (简答题) **查询显示学生的学号、姓名和已经获得的总学分。

提示:

(1)学分=学时/16

(2)选课但没有参加考试或考试不及格的课程,总学分均不统计。

select students.sno,sname,sum(chour)/16

from students,stc,courses

where students.sno=stc.sno 

      and stc.cno=courses.cno

      and score>=60    

group by students.sno;

10. (简答题)将全部2016级女生的C程序设计课程的成绩每人提高10分,并查看修改后的STC表。

update stc

set score =score+10

where sno in (

         select sno from students 

         where ssex='女' and grade='2016' ) 

  and cno =(select cno from courses     --也可以将等号(=)换成in

            where cname='C程序设计'); 

11. (简答题) 将选修“C语言程序设计”的选课记录保存在STC_C表中。

提示:STC_C表尚不存在。

create table stc_c

AS SELECT * FROM stc    ---AS后面也可以用连接查询

     where cno in (

           select cno from courses 

           where cname='C程序设计');

12. (简答题)将选修了“高等数学”课程的选课记录追加到STC_C表中。

insert into stc_c

   SELECT * FROM stc 

   where cno in (

          select cno from courses 

          where cname='高等数学'

       );

13. (简答题)将所有女生的选课成绩置空。

update stc

set score =NULL

where sno in (

    select sno from students

    where ssex='女');

14. (简答题)删除没有被学生选修的课程信息。

delete from courses

where cno not in (

  select cno distinct from stc);

你可能感兴趣的:(数据库,sql)