【Sql Server学习】Sql Server数据查询(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010800530/article/details/44162641

(1)查询信息系(IS)的所有学生的姓名,所选课程号及成绩。

select Sname,Cno,Grade 
from Student,SC 
where Sdept IN
      (select Sdept 
       from Student 
       where Sdept='IS')
 and Student.sno=SC.sno

(2)查询选修“信息系统”且成绩高于90分的学生学号。

select * 
from SC 
where Grade>=90 and Cno=
                   (select Cno
                    from Course 
                    where Cname='信息系统')

(3)查询选修学分为3或4且参加考试的学生学号、课程号。

select Sno,Cno 
from SC 
where Cno in
     (select Cno
      from Course
       where Ccredit in(3,4))

(4)查询其它系中比信息系某一学生年龄大的学生姓名和年龄。

select Sname,Sage 
from Student 
where sage>any
     (select Sage 
      from Student
       where Sdept='IS')

(5)查询每个学生超过他选修课程平均成绩的课程号。

select sname,cno,sc.sno 
from SC,Student 
where grade>all
      (select avg(grade) 
      from SC) 

(6)查询没有选修1号课程的学生姓名。

select sname 
from student 
where not exists 
      (select * 
       from SC 
       where cno='1'and sno=student.sno)

(7)查询选修了全部课程的学生姓名。

select sname 
from student 
where not exists
     (select *
      from course
      where not exists
            (select *
             from sc
              where sno=student.sno and cno=course.cno))

(8)查询既选修了1号课程,又选修了2号课程的学生姓名。

select sname 
from student 
where sno in
      (select sno
       from sc       
       where cno='1' intersect select sno
                               from sc
                               where cno='2')

(9)查询信息系学生及年龄大于20岁的学生(用交实现)

select * 
from student
where sdept='IS' 
except select * 
       from student 
       where sage<20

(10)查询信息系的学生与年龄不大于19岁的学生的差集。

select * 
from student
where sdept='IS' except select * 
                        from student 
                        where sage>19


猜你喜欢

转载自blog.csdn.net/u010800530/article/details/44162641