sql面试题

表名  subject 

表名  course


表名  student

create table student(
       no number(10),
       name varchar2(15),
       sex char(4),
       age number(10),
       dept varchar2(15)

    ); 

create table course (
      no number(10),
      name varchar2(15),
      hosrs number(10)
    );

create table subject(
       student_no number(10),
       course_no number(10),
       grade number(10)
    );




问题1:

  查询学生选修了哪些课程,要求列出课程名,课程号,选修人数,和最好成绩

select max(s.grade),b.name,count(t.no),b.no
  from  course b, subject s,student t
  where b.no=s.course_no
  and t.no=s.student_no

  group by b.name,b.no

问题2:

统计每个学生的选课门数,并按照选课门数递减顺序显示

select t.name,count(s.course_no)
  from course b, subject s, student t
 where b.no = s.course_no
   and t.no = s.student_no
 group by t.name

 order by count(s.course_no) desc 


问题3:

查询选课门数超过2门的学生平均成绩和选课门数

select count(s.course_no),avg(s.grade)
   from course b, subject s, student t
  where b.no = s.course_no
    and t.no = s.student_no
    group by s.student_no

   having count(s.course_no)>2


查询有考试成绩的所有学生,学生姓名,选课名称,和成绩,放到一个新表里re_tal_new.

建表语句 

create table tb_rel_new (

        student_name varchar2(15),
        subject_name varchar2(15),
        grade number(10)

     );   

查询插入脚本

declare
     begin     
     for r in(select t.name student_name,b.name subject_name,s.grade grade
     from course b, subject s, student t
    where b.no = s.course_no
      and t.no = s.student_no) loop
     insert into tb_rel_new(student_name,subject_name,grade)values(r.student_name,r.subject_name,r.grade);
     end loop; 
    end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值