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;