1. 程式人生 > >【Sql Server學習】Sql Server資料查詢(三)

【Sql Server學習】Sql Server資料查詢(三)

(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