数据库练习题1-25题

  1 --1、 查询Student表中的所有记录的Sname、Ssex和Class列
  2 select Sname,Ssex,Class from Student
  3 
  4 --2、 查询教师所有的单位即不重复的Depart列。--distinct 去除重复
  5 select distinct Depart from Teacher
  6 
  7 --3、 查询Student表的所有记录。
  8 select * from Student
  9 
 10 --4、 查询Score表中成绩在60到80之间的所有记录。
 11 select * from  Score where Degree between 60 and 80
 12 
 13 --5、 查询Score表中成绩为85,86或88的记录。
 14 select * from Score where Degree in (85,86,88)
 15 
 16 --6、 查询Student表中“95031”班或性别为“女”的同学记录。
 17 select * from Student where Class='95031'or Ssex=''
 18 
 19 --7、 以Class降序查询Student表的所有记录。
 20 select * from student order by Class desc
 21 
 22 --8、 以Cno升序、Degree降序查询Score表的所有记录。--降序:desc 升序:默认asc
 23 select * from Score order by Cno,Degree desc 
 24 
 25 --9、 查询“95031”班的学生人数。
 26 select COUNT(*)from Student where Class='95031'
 27 
 28 --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
 29 select Sno,Cno from Score where Degree = ( select MAX(Degree) from Score)--子查询法
 30 
 31 select top 1 Sno,Cno from Score  order by Degree desc --排序法,用Top
 32 
 33 --11、 查询每门课的平均成绩。
 34 select Cno,AVG(Degree) from Score group by Cno 
 35 
 36 
 37 --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 38 select Cno,AVG(Degree) from Score  group by Cno having COUNT(*)>5 and Cno like '3%'
 39 
 40  
 41 --13、查询分数大于70,小于90的Sno列。--between/and 是包含,是大于等于,所以这里不能用,要用>,<
 42 select Sno from Score  where Degree > 70 and Degree < 90
 43 
 44 --0014、查询所有学生的Sname、Cno和Degree列。注:如果查询2个表中同样sname 需要分别写出Studen.Sname和Score.Sname 
 45 select Sname,Degree,Cno  from Student join Score on Student.Sno=Score.Sno --把2个表格联合做法
 46 select Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno
 47 
 48 --15.查询所有学生的Sno、Cname和Degree列。
 49 select Student.Sno,Cname,Degree from Student join Score on Student.Sno=Score.Sno join Course on  Course.Cno=Score.Cno 
 50 
 51 --16、查询所有学生的Sname、Cname和Degree列。
 52 select Sname,Cname,Degree from Student join Score on Student.Sno=Score.Sno join Course on  Course.Cno=Score.Cno
 53 
 54 
 55 --17、 查询“95033”班学生的平均分。
 56  select AVG(Degree)  from Student join Score on Student.Sno=Score.Sno group by class having Class='95033' --先把2个图拼起来再分组,再求平均值
 57  
 58 -0018假设使用如下命令建立了一个grade表:
 59 create table grade
 60 (
 61   low int,
 62   upp int,
 63   rank char(1),
 64 )  
 65 go
 66 insert into grade values(90,100,'A')
 67 insert into grade values(80,89,'B')
 68 insert into grade values(70,79,'C')
 69 insert into grade values(60,69,'D')
 70 insert into grade values(0,59,'E')
 71 
 72 select Sno,Cno,RANK from Score,grade where Degree between low and upp --变量可以直接做范围low/upp
 73 
 74 --0019、  查询选修“3-105”课程的成绩并且高于“109”号同学成绩的所有同学的记录。
 75 select Degree from Score where Cno = '3-105' and Degree>(
 76 select Degree from Score where Sno = '109' and Cno ='3-105')
 77 
 78 --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
 79 1.找同学
 80 select sno from score group by sno having COUNT(*)>1
 81 2.找最高分
 82 select MAX(degree) from score where sno in(select sno from score group by sno having COUNT(*)>1)
 83 3.去掉最高分
 84 select sno from score where degree !=(select MAX(degree) from score where sno in(select sno from score group by sno having COUNT(*)>1)) group by sno having COUNT(*)>1 
 85 4.找非最高分学生的信息
 86 select * from score where sno in (select sno from score where degree !=(select MAX(degree) from score where sno in(select sno from score group by sno having COUNT(*)>1)) group by sno having COUNT(*)>1)
 87 
 88 --21、 查询成绩高于学号为“109”并且课程号为“3-105”的成绩的所有记录。
 89 select Sno,Cno,Degree from Score where Degree > (select Degree from Score where Sno=109 and Cno='3-105')
 90 
 91 --22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
 92 select Sno,Sname,Sbirthday from Student where YEAR( Sbirthday) = (select YEAR( Sbirthday) from Student where Sno='108') 
 93 
 94 --23、查询“张旭“教师任课的学生成绩。
 95  select * from Score where Cno = (select Cno from Course where Tno= (select Tno from Teacher where Tname = '张旭'))
 96 
 97 --24、查询选修某课程的同学人数多于5人的教师姓名。
 98 select Tname from Teacher where Tno =( select Tno from Course where Cno = ( select Cno from Score group by Cno having COUNT(*)>5))
 99 
100 --25、查询95033班和95031班全体学生的记录。
101 select * from Student where Class = '95033' or Class = '95031'

 

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