zoukankan      html  css  js  c++  java
  • 45道经典例题答案

     声明啊 因为有些题目个人理解不太一样 所以给的答案也不太一样 希望谅解;

    下面是题目

    表结构:
    drop table student;
    drop table course;
    drop table score;
    drop table teacher;
    
    CREATE TABLE STUDENT
    (SNO VARCHAR(3) NOT NULL, 
    SNAME VARCHAR(4) NOT NULL,
    SSEX VARCHAR(2) NOT NULL, 
    SBIRTHDAY DATE,
    CLASS NUMBER NOT NULL);
    
    CREATE TABLE COURSE
    (CNO VARCHAR(5) NOT NULL, 
    CNAME VARCHAR(10) NOT NULL, 
    TNO VARCHAR(10) NOT NULL);
    
    CREATE TABLE SCORE 
    (SNO VARCHAR(3) NOT NULL, 
    CNO VARCHAR(5) NOT NULL, 
    DEGREE NUMBER NOT NULL);
    
    CREATE TABLE TEACHER 
    (TNO VARCHAR(3) NOT NULL, 
    TNAME VARCHAR(4) NOT NULL, 
    TSEX VARCHAR(2) NOT NULL, 
    TBIRTHDAY DATE NOT NULL, 
    PROF VARCHAR(6), 
    DEPART VARCHAR(10) NOT NULL);
    
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'' ,to_date('1977-09-01','yyyy-mm-dd'),95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'' ,to_date('1975-10-02','yyyy-mm-dd'),95031);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'' ,to_date('1976-01-23','yyyy-mm-dd'),95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'' ,to_date('1976-02-20','yyyy-mm-dd'),95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'' ,to_date('1975-02-10','yyyy-mm-dd'),95031);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'' ,to_date('1974-06-03','yyyy-mm-dd'),95031);
    
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
    
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
    INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
    
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','',to_date('1958-12-02','yyyy-mm-dd'),'副教授','计算机系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','',to_date('1969-03-12','yyyy-mm-dd'),'讲师','电子工程系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','',to_date('1972-05-05','yyyy-mm-dd'),'助教','计算机系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','',to_date('1977-08-14','yyyy-mm-dd'),'助教','电子工程系');
    
    题目:
    1、 查询Student表中的所有记录的Sname、Ssex和Class列。
    2、 查询教师所有的单位即不重复的Depart列。
    3、 查询Student表的所有记录。
    4、 查询Score表中成绩在60到80之间的所有记录。
    5、 查询Score表中成绩为85,86或88的记录。
    6、 查询Student表中“95031”班或性别为“女”的同学记录。
    7、 以Class降序查询Student表的所有记录。
    8、 以Cno升序、Degree降序查询Score表的所有记录。
    9、 查询“95031”班的学生人数。
    10、查询Score表中的最高分的学生学号和课程号。
    11、查询‘3-105’号课程的平均分。
    12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    13、查询最低分大于70,最高分小于90的Sno列。
    14、查询所有学生的Sname、Cno和Degree列。
    15、查询所有学生的Sno、Cname和Degree列。
    16、查询所有学生的Sname、Cname和Degree列。
    17、查询“95033”班所选课程的平均分。
    18、假设使用如下命令建立了一个grade表:
    create table grade
    (low number,
    upp number,
    rank char(1));
    insert into grade values(90,100,'A');
    insert into grade values(80,89,'B');
    insert into grade values(70,79,'C');
    insert into grade values(60,69,'D');
    insert into grade values(0,59,'E');
    commit;
    现查询所有同学的Sno、Cno和rank列。
    19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
    20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
    21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
    22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
    23、查询“张旭“教师任课的学生成绩。
    24、查询选修某课程的同学人数多于5人的教师姓名。
    25、查询95033班和95031班全体学生的记录。
    26、查询存在有85分以上成绩的课程Cno.
    27、查询出“计算机系“教师所教课程的成绩表。
    28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    29、查询选修编号为“3-105“且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”的同学的Cno、Sno和Degree.
    31、查询所有教师和同学的name、sex和birthday.
    32、查询所有“女”教师和“女”同学的name、sex和birthday.
    33、查询成绩比该课程平均成绩低的同学的成绩表。
    34、查询所有任课教师的Tname和Depart.
    35、查询所有未讲课的教师的Tname和Depart. 
    36、查询至少有2名男生的班号。
    37、查询Student表中不姓“王”的同学记录。
    38、查询Student表中每个学生的姓名和年龄。
    39、查询Student表中最大和最小的Sbirthday日期值。
    40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    41、查询“男”教师及其所上的课程。
    42、查询最高分同学的Sno、Cno和Degree列。
    43、查询和“李军”同性别的所有同学的Sname.
    44、查询和“李军”同性别并同班的同学Sname.
    45、查询所有选修“计算机导论”课程的“男”同学的成绩表

    嗯 来一波答案

      1 --select t.*, t.rowid from STUDENT t;
      2 
      3 
      4 --查询Student表中的所有记录的Sname、Ssex和Class列。
      5 --select t.Sname,t.Ssex,t.Class from student t;
      6 
      7 --查询Student表的所有记录。
      8 --select * from student order by class;
      9 
     10 -- 查询Score表中成绩在60到80之间的所有记录。
     11 --select *from score t where t.degree between 60 and 80
     12 
     13  --查询Score表中成绩为85,86或88的记录。
     14  --select * from score t where t.degree in (85,86,88);
     15  
     16 --6、 查询Student表中“95031”班或性别为“女”的同学记录。
     17 --select *from student t where t.class='95031'or t.ssex='女'
     18 
     19 --7、 以Class降序查询Student表的所有记录。
     20 --select *from student t order by class desc
     21 
     22 --8、 以Cno升序、Degree降序查询Score表的所有记录。
     23 --select * from score t order by t.cno ,t.degree desc 
     24 
     25 --9、 查询“95031”班的学生人数。
     26 --select t.class, count(t.class) from student t where t.class='95031' group by class
     27 
     28 --10、查询Score表中的最高分的学生学号和课程号。
     29 --select t.sno, t.cno from score t where t.degree=(select max(degree)from score)
     30 
     31 --11、查询‘3-105’号课程的平均分。
     32 --select avg(t.degree) from score t where t.cno='3-105'
     33 
     34 --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
     35 --select avg(t.degree)from score t where t.cno like '3%' group by t.cno having count(t.cno)>=5
     36 
     37 --13、查询最低分大于70,最高分小于90的Sno列。
     38 -- select t.sno from score t where t.degree between 70 and 90 
     39 
     40 --14、查询所有学生的Sname、Cno和Degree列。
     41 --select sname, cno, degree from student ,score where student.sno=score.sno
     42 
     43 --15、查询所有学生的Sno、Cname和Degree列。
     44 --select sno,cname,degree from course,score where  score.cno=course.cno
     45 
     46 --16、查询所有学生的Sname、Cname和Degree列。
     47 --select sname,cname,degree from course,score ,student where course.cno=score.cno and score.sno=student.sno
     48 
     49 --17、查询“95033”班所选课程的平均分。
     50 --select cno, avg(degree)from score ,student where student.class = '95033'and student.sno=score.sno group by cno
     51 
     52 --18、假设使用如下命令建立了一个grade表:
     53 /*create table grade
     54 (low number,
     55 upp number,
     56 rank char(1));
     57 insert into grade values(90,100,'A');
     58 insert into grade values(80,89,'B');
     59 insert into grade values(70,79,'C');
     60 insert into grade values(60,69,'D');
     61 insert into grade values(0,59,'E');
     62 commit;*/
     63 --现查询所有同学的Sno、Cno和rank列。
     64 --select sno ,cno, rank from score,grade where degree between low and upp 
     65 
     66 --查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
     67 --select x.Cno,x.Sno,x.degree from score x,score y 
     68 --where x.cno='3-105' and x.degree>y.degree and y.sno='109'and y.cno='3-105'; 
     69 
     70 -- 20查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
     71 --select * from score s where sno in (select sno from score group by sno having count(*)>1)
     72 --and degree not in (select max(degree) from score group by sno  )
     73  
     74 
     75 
     76 --21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
     77 --select t.cno, t.sno,t.degree from score t  where  t.cno = '3-105' and t.degree>(select degree from score a where 
     78 --a.sno='109' and a.cno='3-105') 
     79 
     80 --22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
     81 --select sno, sname, sbirthday from student where to_char(sbirthday,'yyyy')=(select to_char (sbirthday,'yyyy') from
     82  --student where sno='108')
     83  
     84 --23、查询“张旭“教师任课的学生成绩。
     85 --select sno,degree from score s ,teacher t ,course where t.tname='张旭' and course.tno=t.tno
     86 
     87 --24、查询选修某课程的同学人数多于5人的教师姓名。
     88 --select  tname from  teacher where tno in (select c.tno from course c, score s where c.cno=s.sno
     89  --group by c.tno having count(c.tno)>5) 
     90 
     91 --25、查询95033班和95031班全体学生的记录。
     92 
     93 --select * from student where class in ('95033','95031')
     94 
     95 --26、查询存在有85分以上成绩的课程Cno.
     96 --select cno from score where degree>85 group by cno
     97 
     98 --27、查询出“计算机系“教师所教课程的成绩表。
     99 --select degree,sno,cno ,t.depart from score, teacher t where cno in ( select c.cno from course c ,teacher t 
    100 --where c.tno=t.tno and t.depart='计算机系')  and t.depart='计算机系'
    101 
    102 --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    103 --select tname, prof from teacher where depart='计算机系' and prof not in 
    104 --(select prof from teacher where depart = '电子工程系')
    105 
    106 --29、查询选修编号为“3-105“且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    107 
    108 
    109 --select  * from score s where s.cno='3-105' and 
    110 --s.degree >all (select s.degree from score s where s.cno='3-245')order by s.degree desc;
    111 
    112 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”的同学的Cno、Sno和Degree.
    113 
    114 --select * from score where cno='3-105' and degree>all(select degree from score where cno='3-245');
    115 
    116 --31、查询所有教师和同学的name、sex和birthday.
    117 /*select tname,tsex,tbirthday from teacher union 
    118 select sname,ssex,sbirthday from student; */
    119 
    120 --32、查询所有“女”教师和“女”同学的name、sex和birthday.
    121 
    122 --select tname,tsex,tbirthday from teacher where tsex='女' union
    123 --select sname, ssex,sbirthday from student where ssex = '女'
    124 
    125 --33、查询成绩比该课程平均成绩低的同学的成绩表。
    126 --select * from score  a where degree<
    127 --(select avg(degree) from score b where a.cno=b.cno); 
    128 
    129 --34、查询所有任课教师的Tname和Depart.
    130 --select tname ,depart from teacher a where exists 
    131 --(select * from course b where a.tno=b.tno); 
    132 --35、查询所有未讲课的教师的Tname和Depart. 
    133 --select tname ,depart from teacher a  where not exists 
    134 --(select * from course b where a.tno=b.tno); 
    135 
    136 --36、查询至少有2名男生的班号。
    137  -- select class from student where ssex='男'group by class having count(*)>=2; 
    138 --37、查询Student表中不姓“王”的同学记录。
    139 --select * from student where sname not like'王%'; 
    140 
    141 --38、查询Student表中每个学生的姓名和年龄。
    142 
    143 --select sname as 姓名,(to_char(sysdate,'yyyy')-to_char(sbirthday,'yyyy')) as 年龄 from student
    144 
    145 
    146 --39、查询Student表中最大和最小的Sbirthday日期值。
    147 
    148 --select sname,sbirthday  from student where sbirthday =(select min (sbirthday) from student)union 
    149 --select sname,sbirthday  from student where sbirthday =(select max(sbirthday) from student)
    150 
    151 --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    152 --select * from student s order by class desc , sbirthday desc;
    153 
    154 
    155 --41、查询“男”教师及其所上的课程。
    156 -- select x.tname,y.cname from teacher x,course y where x.tno=y.tno and x.tsex='男';
    157 
    158 --42、查询最高分同学的Sno、Cno和Degree列。
    159 
    160 --select * from score where degree=(select max(degree)from score); 
    161 
    162 --43、查询和“李军”同性别的所有同学的Sname.
    163 --select sname from student where ssex=(select ssex from student where sname='李军');
    164 
    165 --44、查询和“李军”同性别并同班的同学Sname.
    166 --select sname from student where ssex=(select ssex from student where sname='李军') and
    167  --class=(select class from student where sname='李军')
    168 
    169 --45、查询所有选修“计算机导论”课程的“男”同学的成绩表
    170 
    171 select * from score where sno in(select sno from student where ssex='') and
    172  cno=(select cno from course where cname='计算机导论'); 
  • 相关阅读:
    HYSBZ 3813 奇数国
    HYSBZ 4419 发微博
    HYSBZ 1079 着色方案
    HYSBZ 3506 排序机械臂
    HYSBZ 3224 Tyvj 1728 普通平衡树
    Unity 3D,地形属性
    nginx 的naginx 种包含include关键字
    Redis 出现NOAUTH Authentication required解决方案
    mysql 8.0出现 Public Key Retrieval is not allowed
    修改jar包里的源码时候需要注意的问题
  • 原文地址:https://www.cnblogs.com/xieyunhuaxiang/p/6995208.html
Copyright © 2011-2022 走看看