【SQL Server数据库】连接查询和嵌套查询

目录

1.找出所有任教“数据库”的教师的姓名。

2. 取出学号为“980101011”的学生选修的课程号和课程名。

3.“涂杰杰”所选修的全部课程号及成绩。

4.找出“苏贤兴”同学所学课程的名称和成绩。

5. 显示所有课程的选修情况(外连接)。

6.检索选修课程号为“0109”或“0111”的学生学号、姓名和所在班级。

7.查询“0203”课程的最高分的学生的学号。

8.没有选修以“01”开头的课程的学生信息。


1.找出所有任教“数据库”的教师的姓名。

/*连接查询*/

select distinct t.Teac_name

from Course c,

     Teacher t,

     CourseTeacher ct

where c.Course_name = '数据库'

  and c.Course_id = ct.Course_id

  and t.Teac_id = ct.Teac_id;

/*嵌套查询*/

select Teac_name

from Teacher

where Teac_id in (select Teac_id

                  from CourseTeacher

                  where Course_id in (select Course_id from Course where Course_name = '数据库'));

2. 取出学号为“980101011”的学生选修的课程号和课程名。

/*连接查询*/

select c.Course_id, c.Course_name

from StudentGrade sg,

     Course c

where sg.Stu_id = '980101011'

  and c.Course_id = sg.Course_id;

/*嵌套查询*/

select Course_id, Course_name

from Course

where Course_id in (select Course_id

                    from StudentGrade

                    where Stu_id = '980101011');

3.“涂杰杰”所选修的全部课程号及成绩。

(注意:school中有同名,即有两名学生叫“涂杰杰”。)

/*连接查询*/

select s.Stu_id, sg.Course_id, sg.Grade

from Student s,

     StudentGrade sg

where s.Stu_id = sg.Stu_id

  and s.Stu_name = '涂杰杰';

/*嵌套查询*/

select Stu_id,Course_id, Grade

from StudentGrade

where Stu_id in (select Stu_id from Student where Stu_name = '涂杰杰');

4.找出“苏贤兴”同学所学课程的名称和成绩。

(请使用连接查询和嵌套查询分别来完成)

/*连接查询  -- 左连接*/

select c.Course_name, sg.Grade

from StudentGrade sg

         left join Course c on c.Course_id = sg.Course_id

         left join Student s on s.Stu_id = sg.Stu_id

where s.stu_name = '苏贤兴';



/*嵌套查询*/

select c.Course_name, sg.Grade

from Course c,

     StudentGrade sg

where sg.Course_id = c.Course_id

  and sg.Stu_id in (select Stu_id

                    from Student

                    where Stu_name = '苏贤兴');

5. 显示所有课程的选修情况(外连接)。

/*连接查询 没有人选的课也要展示出来*/

select c.Course_id, c.Course_name, count(sg.Stu_id) '选修人数'

from Course c

         left join StudentGrade sg on (c.Course_id = sg.Course_id)

group by c.Course_id, c.Course_name;

/*嵌套查询*/

select c.Course_id,

       c.Course_name,

       (select count(sg.Stu_id) '选修人数' from StudentGrade sg where sg.Course_id = c.Course_id)

from Course c;

6.检索选修课程号为“0109”或“0111”的学生学号、姓名和所在班级。

/*连接查询*/

select distinct s.Stu_id, s.Stu_name, c.Class_name

from Student s,

     Class c,

     StudentGrade sg

where (sg.Course_id = '0109' or sg.Course_id = '0111')

  and s.Class_id = c.Class_id

  and sg.Stu_id = s.Stu_id;

/*嵌套查询*/

select s.Stu_id, s.Stu_name, c.Class_name

from Student s,

     Class c

where C.Class_id = s.Class_id

  and Stu_id in (select Stu_id from StudentGrade where Course_id = '0109' or Course_id = '0111');

7.查询“0203”课程的最高分的学生的学号

/*连接查询*/

select Stu_id

from StudentGrade sg,

     (select max(Grade) macGrade from StudentGrade where Course_id = '0203') mG

where sg.Grade = mG.macGrade

  and sg.Course_id = '0203';



/*嵌套查询*/

select Stu_id

from StudentGrade

where Course_id = '0203'

  and Grade =

      (select max(Grade)

       from StudentGrade

       where Course_id = '0203');

8没有选修以“01”开头的课程的学生信息。

(用子查询完成,提示not in或not exists。需考虑没选课的学生)

/*not exists*/

select distinct *

from Student s

where not exists(select * from StudentGrade sg where Course_id like '01%' and sg.Stu_id = s.Stu_id);

/*not in*/

select *

from Student

where Stu_id not in (select Stu_id from StudentGrade where Course_id like '01%');

思考

1. 简单查询、连接查询与嵌套查询有什么不同?

若一个查询只涉及一个表,则称之为简单查询

若一个查询同时涉及两个或两个以上的表,则称之为连接查询

嵌套查询又称子查询,是指在父查询的where条件语句中再插入一个子查询语句。

2. 连接查询与嵌套查询有何区别与联系?

区别:

① 含义上的不同

嵌套查询是包含一个或多个子查询或者子查询的另一个术语的SELECT语句。在一个外层查询中包含有另一个内层查询子查询是SQL语句的扩展。

连接查询是关系数据库中最主要的查询。在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息。

② 特性上的不同

嵌套查询的主要特性是SQL允许多层嵌套,是由内而外地进行分析,子查询的结果作为主查询的查询条件。

连接是关系数据库模型的主要特点,通过连接运算符可以实现多个表查询。连接操作给用户带来很大的灵活性,可以在任何时候增加新的数据类型。

联系:连接查询都可以用嵌套查询完成,反之不然。

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值