SQL题目5 - oracle

student
sno - 学号 - 外键
sname - 学生姓名
sage - 学生年龄
ssex - 学生性别
teacher
tno - 教师工号 - 外键
tname - 教师姓名
course
cno - 课程号
tno - 教师工号 - 课程号与工号共同构成主键
cname - 课程名
sc
sno - 学生学号
cno - 课程号 - 学号、课程号共同构成主键
score - 分数

1. 创表、初始化数据 - 这部分读者自行复制完成

student、teacher、course、sc - 四个表

-- 创student表
create table student(
    sno varchar2(10) primary key,
    sname varchar2(20),
    sage number(2),
    ssex varchar2(5)
);
-- 往 student 表添加数据
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
commit;

-- 创teacher表
create table teacher(
    tno varchar2(10) primary key,
    tname varchar2(20)
);
-- 往 teacher 表添加数据
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');
commit;

-- 创course表
create table course(
    cno varchar2(10),
    cname varchar2(20),
    tno varchar2(20),
    constraint pk_course primary key (cno,tno)
);
-- 往 course 表添加数据
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');
commit;


-- 创sc表
create table sc(
    sno varchar2(10),
    cno varchar2(10),
    score number(4,2),
    constraint pk_sc primary key (sno,cno)
);
-- 往 sc 表添加数据
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c001',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003','59');
commit;


2. SQL题目练习 - 基于上面的表、数据

  1. 查询“c001”课程比“c002”课程成绩高的所有学生的学号
select sno 
    from sc sc1
    where cno = 'c001'
        and score > (select score 
                                  from sc sc2 
                                  where cno = 'c002'
                                    and sc2.sno = sc1.sno );

  1. 查询平均成绩大于60 分的同学的学号和平均成绩
select sno, avg(score)
    from sc
    group by sno
    having avg(score) > 60;

  1. 查询所有同学的学号、姓名、选课数、总成绩
select student.sno, sname, count(cno), sum(nvl( score, 0))
    from student, sc
    where student.sno = sc.sno(+)
    group by student.sno, sname;

  1. 查询姓“刘”的老师的个数
select count(*)
    from teacher
    where instr(tname, '刘') = 1;

  1. 查询没学过“谌燕”老师课的同学的学号、姓名
select distinct student.sno, sname
    from student, sc
    where student.sno = sc.sno(+)
        and student.sno not in ( select sno 
                                                    from sc 
                                                    where cno in ( select cno 
                                                                                    from course, teacher
                                                                                    where course.tno = teacher.tno
                                                                                        and tname = '谌燕') );  

  1. 查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名
select sno, sname
    from student stu
    where exists (select * from sc where cno = 'c001' and stu.sno = sc.sno)
        and exists (select * from sc where cno='c002' and stu.sno = sc.sno) ;

  1. 查询学过“谌燕”老师所教的所有课的同学的学号、姓名
select sno, 
          (select sname from student where sno = sc.sno)
    from sc
    where cno in  ( select cno 
                                  from course, teacher
                                  where course.tno = teacher.tno
                                      and tname = '谌燕')
    group by sno
    having count(*) =  ( select count(*) 
                                  from course, teacher
                                  where course.tno = teacher.tno
                                      and tname = '谌燕')

  1. 查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名
select sno, 
          (select sname from student where sno = sc1.sno)
     from sc sc1
     where cno = 'c002'
        and exists (select * 
                                from sc 
                                where cno = 'c001'
                                    and sno = sc1.sno
                                    and score > sc1.score);

  1. 查询所有课程成绩小于60 分的同学的学号、姓名
select sno,
          (select sname from student where sno = sc.sno)
    from sc
    where sno not in ( select distinct sno from sc where score >= 60 );

  1. 查询没有学全所有课的同学的学号、姓名
select sno, sname
    from student
    where not exists ( select  distinct sno
                                      from sc
                                      where sno = student.sno 
                                      group by sno
                                      having count(*) = ( select count(*) from course));

  1. 查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名
select distinct sno, 
          (select sname from student where sno = sc.sno)
   from sc
   where cno in (select cno from sc where sno = 's001')
      and sno != 's001';

  1. 查询至少学过学号为“s001”同学所有课的其他同学学号和姓名
select sno,
            (select sname from student where sno = sc.sno)
    from sc
    where sno != 's001'
        and cno in (select cno from sc where sno = 's001')
    group by sno
    having count(*) = (select count(*) from sc where sno = 's001');

  1. 把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩
update sc sc1 set score = ( select avg(score) from sc sc2 where sc2.cno = sc1.cno ) 
    where cno in ( select cno from course
                                where tno = ( select tno from teacher
                                                               where tname = '谌燕'));
                                                               
                                                               
rollback;    ------------  记得回滚数据,我们不提交更改原始数据                                                               

  1. 查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名
select sno,
            (select sname from student where sno = sc.sno)
    from sc
    where sno != 's001'
        and cno in (select cno from sc where sno = 's001')
    group by sno
    having count(*) = (select count(*) from sc where sno = 's001');

  1. 删除学习“谌燕”老师课的SC 表记录
delete table sc 
    where cno in ( select cno from course
                                where tno = ( select tno from teacher
                                                               where tname = '谌燕' ));

  1. 向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩
insert into sc values ('s004',  'c006', (select avg(score) from sc where cno = 'c002') );
insert into sc values ('s007', 'c007', (select avg(score) from sc where cno = 'c002') );
insert into sc values ('s010', 'c010', (select avg(score) from sc where cno = 'c002') );
insert into sc values ('s009', 'c009', (select avg(score) from sc where cno = 'c002') );
commit;


--- 额外增加的 - 与题目无关 -- 方便查看下列题目输出  --  建议读者添加这些数据
insert into sc values ('s006','c003', 90 );   
insert into sc values ('s007', 'c001', 81.9);
insert into student values ('s011', '周三'27'男');
insert into student values ('s012', '林三', 22, '女');
insert into student values ('s013', '张国量', 38, '男');
insert into sc values ('s001', 'c004', 30);
insert into sc values ('s001', 'c005', 20);
insert into sc values ('s005', 'c004', 58);
insert into sc values ('s005', 'c008', 57);
commit;

  1. 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select course.cno, nvl(max(score), 0), nvl(min(score), 0)
    from course, sc
    where course.cno = sc.cno (+)
    group by course.cno
    order by course.cno;

  1. 按各科平均成绩从低到高和及格率的百分数从高到低顺序
select (select cname from course where cno = sc1.cno ),
           ( ( select count(*) from sc sc2 where sc2.cno = sc1.cno and score >= 60) / count(*)) * 100 || '%'
   from sc sc1
   group by cno
   order by avg(score) desc;

  1. 查询不同老师所教不同课程平均分从高到低显示
select (select tno from course where cno = sc.cno),
          (select cname from course where cno = sc.cno),
          avg(score)
    from sc
    group by cno;
    

  1. 统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select distinct cno,
          ( select cname from course where cno = sc1.cno ),
         ( select count(*) from sc sc2 where sc2.cno = sc1.cno and  score between 85 and 100 ),
         ( select count(*) from sc sc2 where sc2.cno = sc1.cno and  score between 75 and 84.5),
         ( select count(*) from sc sc2 where sc2.cno = sc1.cno and  score between 60 and 69.5),
         ( select count(*) from sc sc2 where sc2.cno = sc1.cno and  score < 60 )
    from sc sc1;

  1. 查询各科成绩前三名的记录:(不考虑成绩并列情况)
select *
    from ( select row_number() over (partition by cno order by score desc) rn,
                            sc.*
                      from sc)
    where rn <= 3;

  1. 查询每门课程被选修的学生数
select cno,
          ( select nvl( count(*), 0 ) from sc where cno = course.cno )
    from course;   

  1. 查询出只选修了一门课程的全部学生的学号和姓名
select sno,
            (select sname from student where sno = sc.sno)
    from sc
    group by sno
    having count(*) = 1;

  1. 查询男生、女生人数
select ssex,
          count(*)
    from student
    group by ssex;

  1. 查询姓“张”的学生名单
select *
    from student
    where sname like '张%';

  1. 查询同名同性学生名单,并统计同名人数
select stu1.*
    from student stu1
    where exists ( select * from student stu2
                                where stu2.sno != stu1.sno
                                    and stu2.ssex = stu1.ssex
                                    and substr(stu1.sname, 2) = substr( stu2.sname, 2) ) ;

---- 这题实在搞不懂为什么写在一条,硬写在一条实在是难看,所以分两条写

select count(*) 同名人数
    from ( select stu1.*
                    from student stu1
                    where exists ( select * from student stu2
                                                where stu2.sno != stu1.sno
                                                    and substr(stu1.sname, 2) = substr( stu2.sname, 2)) );

  1. 1981 年出生的学生名单(注:Student 表中Sage 列的类型是number)
select *
    from student
    where sage = (extract( year from sysdate ) - 1981 );

  1. 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select course.cno, nvl( avg(score), 0 )
    from course, sc
    where course.cno = sc.cno(+)
    group by course.cno
    order by nvl( avg(score), 0 ) asc, course.cno desc;

  1. 查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
select sc.sno, sname, nvl( avg(score), 0 )
    from student, sc
    where student.sno = sc.sno
    group by sc.sno, sname
    having  avg(score) > 85;

  1. 查询课程名称为“数据库”,且分数低于60 的学生姓名和分数
select sname, score
    from student, sc
    where student.sno = sc.sno
        and score < 60
        and cno = ( select cno from course where cname = '数据库' );

  1. 查询所有学生的选课情况
select student.*, cno
    from student, sc
    where student.sno = sc.sno(+);

  1. 查询任何一门课程成绩在70 分以上的姓名、课程名称和分数
select ( select sname from student where sno = sc.sno ),
          ( select cname from course where cno = sc.cno ),
          score
    from sc
    where score > 70;

  1. 查询不及格的课程,并按课程号从大到小排列
select *
    from sc
    where score < 60
    order by cno desc;

  1. 查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名
select sno,
          ( select sname from student where sno = sc.sno ),
          score
    from sc
    where cno = 'c001'
        and score > 80;

  1. 求选了课程的学生人数
select count( distinct sno )
    from sc

  1. 查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select ( select sname from student where sno = sc.sno )
          score
    from sc, course
    where sc.cno = course.cno
        and tno in ( select tno from teacher where tname = '谌燕' )
        and score = ( select max(score) from sc sc1, course c1
                                    where sc1.cno = c1.cno
                                        and tno in ( select tno from teacher where tname = '谌燕' ) );

  1. 查询各个课程及相应的选修人数
select course.cno, nvl( count(sno), 0 )
    from sc, course
    where course.cno = sc.cno(+)
    group by course.cno;

  1. 查询不同课程成绩相同的学生的学号、课程号、学生成绩
select sno, cno, score
    from sc sc1
    where exists ( select * from sc sc2
                                where  sc2.cno != sc1.cno
                                    and sc2.score = sc1.score);

  1. 查询每门功课成绩最好的前两名
select *
    from sc sc1
    where ( select count(distinct score) from sc sc2 
                        where sc2.cno = sc1.cno
                            and sc2.score > sc1.score ) <= 1
    order by cno asc, score desc;

  1. 统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno, count(*) s_number
    from sc
    group by cno
    having count(*) > 10
    order by s_number desc, cno asc;

  1. 检索至少选修两门课程的学生学号
select sno
    from sc
    group by sno
    having count(*) >= 2;

  1. 查询全部学生都选修的课程的课程号和课程名
select sc.cno, cname
    from sc, course
    where sc.cno = course.cno
    group by sc.cno, cname
    having count(*) = ( select count(*) from student );

  1. 查询没学过“谌燕”老师讲授的任一门课程的学生姓名
select ( select sname from student where sno = sc.sno )
    from sc, course
    where sc.cno = course.cno
        and tno != ( select tno  from teacher where tname = '谌燕');

  1. 查询两门以上不及格课程的同学的学号及其平均成绩
select sno, avg(score)
    from sc
    where score < 60
    group by sno
    having count(*) >= 2;

  1. 检索“c004”课程分数小于60,按分数降序排列的同学学号
select sno
    from sc
    where cno = 'c004'
        and score < 60;

  1. 删除“s002”同学的“c001”课程的成绩
delete  sc 
    where sno = 's002'
        and cno = 'c001';
        
rollback;   ---  不建议commit真正删除数据        
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值