美文网首页
【SQL】5.test练习题及答案(2)

【SQL】5.test练习题及答案(2)

作者: 一曈 | 来源:发表于2017-06-05 13:00 被阅读173次

    18、假设使用如下命令建立了一个grade表:

    create table grade(low number(3,0),up number(3),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’);

    现查询所有同学的Sno、Cno和rank列

    select sc.sno,sc.cno,g.rank from score sc
    join grade g
    on/where sc.degree between g.low and g.up;
    
    题目18

    19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

    select a.* from score a where a.cno='3-105' and a.degree>all(select degree
    from score b where b.sno='109' and b.cno='3-105');
    
    题目19

    20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录

    select * from score sc where degree<(select max(degree) from score) 
    group by sno having count(sno)>1
    order by degree;
    
    题目20

    21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
    <b>同19题</b>
    22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列

    select sno,sname,sbirthday from student where year(sbirthday)=
    (select year(sbirthday) from student where sno='108');
    
    题目22

    23、查询“张旭“教师任课的学生成绩

    select sc.sno,sc.degree from score sc
    join(teacher t,course c)
    on sc.cno=c.cno and t.tno=c.tno
    where t.tname='张旭';
    
    题目23

    24、查询选修某课程的同学人数多于5人的教师姓名

     select t.tname from teacher t join (course c,score sc)
     on (t.tno=c.tno and c.cno=sc.cno)
     group by sc.cno having count(sc.cno) >5;
    
    题目24

    25、查询95033班和95031班全体学生的记录

    select * from student
    where class in ('95033','95031');
    
    题目25

    26、查询存在有85分以上成绩的课程Cno

    解法一
    select distinct cno from score where degree > 85;
    
    解法二
     select cno from score group by cno having max(degree)>85;
    
    题目26

    27、查询出“计算机系“教师所教课程的成绩表

    解法一
    select sc.* from score sc
    join (teacher t,course c)
    on sc.cno = c.cno and t.tno=c.tno
    where t.depart = '计算机系';
    
    解法二
    select * from score where cno in
    (select c.cno from course c join teacher t
    on c.tno=t.tno and t.depart='计算机系');
    
    题目27

    28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof

     select tname,prof from teacher 
    where depart='计算机系' and prof not in 
    (select prof from teacher where depart='电子工程系');
    
    题目28

    29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

    select cno,sno,degree from score
    where degree >=(select min(degree) from score where cno='3-245')
    <!-- any(select degree)-->
    and cno='3-105'
    order by degree desc;
    
    题目29

    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

    select cno,sno,degree from score
    where degree >=(select max(degree) from score where cno='3-245')
    and cno='3-105'
    order by degree desc;
    
    题目30

    31、查询所有教师和同学的name、sex和birthday.

    <!-- as 可以去掉-->
    select sname as name,ssex as sex,sbirthday as birthday from student
    union
    select tname as name,tsex as sex,tbirthday as birthday from teacher;
    
    题目31

    32、查询所有“女”教师和“女”同学的name、sex和birthday.

    <!-- as 可以去掉-->
    select sname as name,ssex as sex,sbirthday as birthday from student
    where ssex = '女'
    union
    select tname as name,tsex as sex,tbirthday as birthday from teacher
    where tsex = '女';
    
    题目32

    33、查询成绩比该课程平均成绩低的同学的成绩表。

    select sc.* from score sc 
    where degree <(select avg(degree) from score sc2 
    where sc.cno=sc2.cno);
    
    题目33

    34、查询所有任课教师的Tname和Depart.

    select t.tname,t.depart from teacher t 
    join course c on t.tno=c.tno;
    
    题目34

    35、查询所有未讲课的教师的Tname和Depart.

    关联字段名相同可用using,等同于 a.字段1=b.字段1
    select tname,depart from teacher t 
    left join course c using(tno) where isnull (c.tno);
    
    题目35

    36、查询至少有2名男生的班号。

    select class from student s where ssex='男'
    group by class having count(ssex)>1;
    
    题目36

    37、查询Student表中不姓“王”的同学记录

    select * from student where sname not like '王%';
    
    题目37

    38、查询Student表中每个学生的姓名和年龄

     select sname,(year(now())-year(sbirthday)) age from student;
    
    题目38

    39、查询Student表中最大和最小的Sbirthday日期值

    select sname,sbirthday from student where sbirthday = (select max(sbirthday) from student) 
    union
    select sname,sbirthday from student where sbirthday = (select min(sbirthday) 
    from student);
    
    题目39

    40、以班号和年龄从大到小的顺序查询Student表中的全部记录

    select class,(year(now())-year(sbirthday)) age from student 
    order by class desc,age desc;
    
    题目40

    41、查询“男”教师及其所上的课程

    select t.tname,c.cname from teacher t 
    join course c using(tno) where t.tsex='男';
    
    题目41

    42、查询最高分同学的Sno、Cno和Degree列

    select sno,cno,degree from score 
    where degree = (select max(degree) from score);
    
    题目42

    43、查询和“李军”同性别的所有同学的Sname

    select sname from student
    where ssex = (select ssex from student where sname='李军');
    
    select s.sname from student s
    where ssex = (select s2.ssex from student s2 where s2.sname='李军');
    
    题目43

    44、查询和“李军”同性别并同班的同学Sname.

    select sname from student 
    where ssex=(select ssex from student where sname='李军')
    and class=(select class from student where sname='李军');
    
    select a.sname from student a
    where ssex=(select b.ssex from student b where b.sname='李军')
    and class=(select c.class from student c where c.sname='李军');
    
    题目44

    45、查询所有选修“计算机导论”课程的“男”同学的成绩表

    解法1
    select * from score 
    where sno in(select sno from student where ssex='男') 
    and cno=(select cno from course where cname='计算机导论');
    
    解法2
    select sc.* from score sc
    join (student s,course c) using(sno,cno)
    where s.ssex='男'and c.cname='计算机导论';
    
    题目45

    相关文章

      网友评论

          本文标题:【SQL】5.test练习题及答案(2)

          本文链接:https://www.haomeiwen.com/subject/imcifxtx.html