数据库练习题26-45题

 1 --26、  查询存在有85分以上成绩的课程Cno.
 2 select distinct Cno from Score where Degree >85
 3 
 4 --27、查询出“计算机系“教师所教课程的成绩表。
 5 select * from Score where Cno in ( select Cno from Course where Tno in ( select Tno from Teacher where Depart = '计算机系'))
 6 
 7 
 8 --0028、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
 9 select Tname,Prof from Teacher t1 where Depart = '计算机系' and not exists (select * from Teacher t2 where Depart = '电子工程系' and t1.Prof = t2.prof)
10 union
11 select Tname,Prof from Teacher t1 where Depart = '电子工程系' and not exists (select * from Teacher t2 where Depart = '计算机系' and t1.Prof = t2.prof)
12 
13 --0029、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。--any 任何一个值 , all 所有的数值
14 select * from Score where Degree >any( select Degree from Score where Cno='3-245') and Cno ='3-105'  order by  Degree desc
15 
16 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
17 select * from Score where Degree > all(select Degree from Score where Cno='3-245') and Cno='3-105' 
18 
19 --31、 查询所有教师和同学的name、sex和birthday.
20     select Sname,Ssex,Sbirthday from Student
21 union
22     select Tname,Tsex,Tbirthday from Teacher
23 
24 --32、查询所有“女”教师和“女”同学的name、sex和birthday.
25     select Sname,Ssex,Sbirthday from Student where Ssex=''
26 union
27     select Tname,Tsex,Tbirthday from Teacher where Tsex=''
28 
29 --33、 查询成绩比该课程平均成绩低的同学的成绩表。
30     select * from Score s1 where Degree<(select AVG(degree) from Score s2 group by Cno having s1.Cno= s2.Cno)
31 
32 --34、 查询所有任课教师的Tname和Depart.
33     select Tname,Depart from Teacher where Tno in ( select Tno from Course where Cno in (select Cno from Score))
34 
35 --35 、 查询所有未讲课的教师的Tname和Depart. 
36     select Tname,Depart from Teacher where Tno not in ( select Tno from Course where Cno in (select Cno from Score))
37 
38 --36、查询至少有2名男生的班号。--先选出所有男生,再按班级分组
39     select Class from Student where Ssex='' group by Class having COUNT(*)>1
40 
41 --37、查询Student表中不姓“王”的同学记录。  
42     select * from Student where Sname not like '王%'
43 
44 --0038、查询Student表中每个学生的姓名和年龄。
45      --注:year(getdate())获取当前时间“年” getdate()获取
46     select Sname,year(getdate())-YEAR(Sbirthday) as age from Student 
47 
48 --39、查询Student表中最大和最小的Sbirthday日期值。
49      select MAX(Sbirthday),MIN(Sbirthday) from Student
50 
51 --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
52     select  *  from Student  order by Class desc,Sbirthday asc
53  
54 --41、查询“男”教师及其所上的课程。
55     select Cname,Tname from Course join Teacher on Course.Tno=Teacher.Tno where Tsex='' --联合2个表格做法
56     
57 select * from course where tno in(select tno from teacher where tsex='')    --where做法
58 
59 --42、查询最高分同学的Sno、Cno和Degree列。
60     select Sno,Cno,Degree from Score where Degree =( select MAX(Degree) from Score )--方法一
61 
62     select top 1 * from score order by degree desc     --方法二
63 
64 --43、查询和“李军”同性别的所有同学的Sname.
65     select Sname from Student where Ssex=( select Ssex from Student  where Sname='李军') and Sname !='李军'
66 
67 --44、查询和“李军”同性别并同班的同学Sname.
68     select Sname from Student where Ssex=( select Ssex from Student  where Sname='李军') and Class=(select Class from Student where Sname='李军') and Sname !='李军' --方法一
69 
70 select sname from student s1 where ssex=(
71 select ssex from student s2 where sname='李军' and s1.class= s2.class
72 )--方法二    
73 
74 --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
75     select * from Score where Cno=( select Cno from Course where Cname='计算机导论') and Sno in (select Sno from Student where Ssex='')--f方法1

 

posted @ 2015-07-21 20:38  Franky2015  阅读(398)  评论(0编辑  收藏  举报