sql面试题(学生表_课程表_成绩表_教师表@oracle

表架构

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
建表语句

CREATE TABLE student 
  ( 
     s#    INT, 
     sname varchar2(32), 
     sage  INT, 
     ssex  varchar2(8) 
  ) ;
CREATE TABLE course 
  ( 
     c#    INT, 
     cname nvarchar(32), 
     t#    INT 
  );
CREATE TABLE sc 
  ( 
     s#    INT, 
     c#    INT, 
     score INT 
  ) ;
CREATE TABLE teacher 
  ( 
     t#    INT, 
     tname varchar2(16) 
  ) ;

插入测试数据

insert into Student select 1,'刘一',18,'' FROM DUAL union  
select 2,'钱二',19,'' FROM DUAL union 
select 3,'张三',17,'' FROM DUAL union  
select 4,'李四',18,'' FROM DUAL union  
select 5,'王五',17,'' FROM DUAL union  
select 6,'赵六',19,'' FROM DUAL;

insert into Course select 1,'语文',1 from dual union all
 select 2,'数学',2 from dual union all
 select 3,'英语',3 from dual union all
 select 4,'物理',4 from dual;
 
insert into Teacher select 1,'叶平' from dual union all
select 2,'贺高' from dual union all
select 3,'杨艳' from dual union all
select 4,'周磊' from dual;

insert into SC 
select 1,1,56 from dual union all 
select 1,2,78 from dual union all 
select 1,3,67 from dual union all 
select 1,4,58 from dual union all 
select 2,1,79 from dual union all 
select 2,2,81 from dual union all 
select 2,3,92 from dual union all 
select 2,4,68 from dual union all 
select 3,1,91 from dual union all 
select 3,2,47 from dual union all 
select 3,3,88 from dual union all 
select 3,4,56 from dual union all 
select 4,2,88 from dual union all 
select 4,3,90 from dual union all 
select 4,4,93 from dual union all 
select 5,1,46 from dual union all 
select 5,3,78 from dual union all 
select 5,4,53 from dual union all 
select 6,1,35 from dual union all 
select 6,2,68 from dual union all 
select 6,4,71 from dual;
 
insert into SC
select 1,1,56 from dual union all select 1,2,78 from dual union all select 1,3,67 from dual union all select 1,4,58 from dual union all select 2,1,79 from dual union all select 2,2,81 from dual union all select 2,3,92 from dual union all select 2,4,68 from dual union all select 3,1,91 from dual union all select 3,2,47 from dual union all select 3,3,88 from dual union all select 3,4,56 from dual union all select 4,2,88 from dual union all select 4,3,90 from dual union all select 4,4,93 from dual union all select 5,1,46 from dual union all select 5,3,78 from dual union all select 5,4,53 from dual union all select 6,1,35 from dual union all select 6,2,68 from dual union all select 6,4,71 from dual;

问题

1、查询1课程比2课程成绩高的所有学生的学号;
select a.s# from
(select s#,score from sc where c#=1) a,
(select s#,score from sc where c#=2) b
where a.s#=b.s# and a.score>b.score;

2、查询平均成绩大于60分的同学的学号和平均成绩; 
select s#,avg(score) from sc  having avg(score)>60 group by s# order by s#

having 弥补where 不能与 聚合函数连用(即where后面不跟聚合函数)

3、查询所有同学的学号、姓名、选课数、总成绩;
select sc.s#,student.sname,count(c#),sum(score) from sc ,student  where 
sc.s# = student.s# group by sc.s#,student.sname order by s#;
 
select Student.S#,Student.Sname,count(SC.C#),sum(score) 
  from Student left Outer join SC on Student.S#=SC.S# 
  group by Student.S#,Sname  order by student.s#

4、查询姓“李”的老师的个数; 
  select count(Tname) 
  from Teacher 
  where Tname like '李%'; 

5、查询没学过“叶平”老师课的同学的学号、姓名; 
  select Student.S#,Student.Sname 
from Student  
where S# not in (select distinct( SC.S#) from SC,Course,Teacher where  SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 

select s#,sname from STUDENT where S# not in  (select STUDENT.s# from STUDENT,SC where  STUDENT.S#=SC.S# and SC.c#= 
(select c# from course where t#=(select t# from TEACHER where tname='叶平')));

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
 select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#=1 and 
exists( select * from sc sc2 WHERE sc2.c#=2 and sc2.s#=SC.s#); 


7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select STUDENT.s#,STUDENT.SNAME from STUDENT,SC where STUDENT.s#=SC.S# and SC.C#=(
select c# from COURSE,TEACHER where COURSE.T#=TEACHER.T# and TEACHER.TNAME='叶平')

8、查询课程编号2的成绩比课程编号1课程低的所有同学的学号、姓名;
select a.s#,STUDENT.SNAME from STUDENT,
(select s#,score from sc where c#=2) a,
(select s#,score from sc where c#=1) b
where  STUDENT.S#=a.s# and a.s#=b.s# and a.score<b.score ;

9、查询所有课程成绩小于60分的同学的学号、姓名;
select STUDENT.s#,sname from sc,STUDENT where SC.S#=STUDENT.s# and
 score<60 group by STUDENT.s#,sname order by s#;

10、查询没有学全所有课的同学的学号、姓名;
select s#,sname from STUDENT where s# in (select sc.s#  from sc group by SC.s#
 HAVING count(SC.c#) < (select count(COURSE.c#) from COURSE) ) order by s#


11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select SC.s#,STUDENT.sname from sc,STUDENT where SC.s#=STUDENT.s# 
and c# in (select c# from sc where s#=1) group by SC.s# ,STUDENT.SNAME order by SC.s# 

 

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩; 
update sc set  score =
(select avg(score) from SC,COURSE,TEACHER where COURSE.t#=TEACHER.t# and sc.c#=COURSE.c# and TNAME='叶平')
where c#=(select DISTINCT SC.C# from SC,COURSE,TEACHER where COURSE.t#=TEACHER.t# and sc.c#=COURSE.c# and TNAME='叶平');


14、查询和1号的同学学习的课程完全相同的其他同学学号和姓名; 
  select S# from SC where C# in (select C# from SC where S#=1)
 group by S# having count(*)=(select count(*) from SC where S#=1);

15、删除学习“叶平”老师课的SC表记录;

17、按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门课程成绩
select st.s# as 学号,
(select ss.score from sc ss where st.s#=ss.s# and c#=1) as 语文,
(select ss.score from sc ss where st.s#=ss.s# and c#=2) as 数学,
(select ss.score from sc ss where st.s#=ss.s# and c#=3) as 英语,
(select ss.score from sc ss where st.s#=ss.s# and c#=4) as 物理,
avg(ss2.score) as 平均成绩
from student st INNER JOIN sc ss2 on st.s#=ss2.s#
group by st.s#
order by avg(ss2.score) desc

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select c#,max(score),min(score) from sc group by c#;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT
    co.CNAME AS 科目名称,
    avg (ss.score) as 平均成绩,
    CAST (CAST (SUM(case WHEN COALESCE(ss.score,0)>=60 THEN 1 ELSE 0 END) AS FLOAT)/
    COUNT(CO.cname) AS DECIMAL (10, 2))  as 及格率
FROM sc ss ,course co where ss.c# = co.c# GROUP BY co.cname;

 SELECT
    co.CNAME AS 科目名称,
    avg (ss.score) as 平均成绩,
    CAST (CAST (SUM(case WHEN COALESCE(ss.score,0)>=60 THEN 1 ELSE 0 END) AS FLOAT)/
    COUNT(CO.cname) AS DECIMAL (10, 2))  as 及格率
FROM sc ss INNER JOIN course co
on ss.c# = co.c# GROUP BY co.cname;

24、查询学生平均成绩及其名次



25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

26、查询每门课程被选修的学生数 
select (select cname from course where c#=SC.c#) 课程,count(SC.C#) 学生数 from sc group by SC.c# ;                                      
 
27、查询出只选修了一门课程的全部学生的学号和姓名
select SC.S#,SNAME from SC ,STUDENT where SC.S#=STUDENT.S#  group by SC.S#,SNAME  HAVING count(c#)=1 ;

28、查询男生、女生人数
select a.男生,b.女生 from
(select count(*) 男生 from STUDENT  WHERE SSEX='男') a,
(select count(*) 女生 from STUDENT  WHERE SSEX='女') b;

select SSEX,count(SSEX) from STUDENT group by ssex;

29、查询姓“张”的学生名单
 select sname from STUDENT where sname like '张%';
  30、查询同名同性学生名单,并统计同名人数
select sname from STUDENT group by sname HAVING count(*) >1;

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select c#,avg(score) from sc group by c# order by avg(score) ,c# desc;

33、查询平均成绩大于70的所有学生的学号、姓名和平均成绩
select SC.s#, sname, avg(score) from sc,STUDENT  where SC.S#=STUDENT.S#  group by SC.s#,sname HAVING avg(score)>70

34、查询课程名称为“语文”,且分数低于60的学生姓名和分数
select SC.s#,sname,score from sc,COURSE,STUDENT where SC.s#=STUDENT.s# and SC.C#=COURSE.C# and COURSE.cname='语文' and SCORE<60;

35、查询所有学生的选课情况
select st.sname,co.cname,ss.score
from sc ss,student st,course co where ss.s#=st.s# and ss.c#=co

select st.sname,co.cname,ss.score
from sc ss
inner join student st on ss.s#=st.s#
inner join course co on ss.c#=co.c#

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select sname,cname,score from sc
INNER JOIN STUDENT on STUDENT.S#=SC.S#
INNER JOIN COURSE on COURSE.c#=SC.c# and score>70;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select STUDENT.SNAME,SC.SCORE from STUDENT,sc where STUDENT.s#=SC.S# and score=
(select max(SC.SCORE) from STUDENT,SC,COURSE,TEACHER  where COURSE.T#=TEACHER.T#
and TEACHER.TNAME='叶平' and COURSE.C#=SC.C# )

select st.sname,co.cname,te.tname, ss.score from sc ss
inner join student st on ss.s#=st.s#
inner join course co on ss.c#=co.c#
inner join teacher te on co.t#=te.t#
where te.tname='叶平' order by ss.score desc

41、查询各个课程及相应的选修人数
select COURSE.CNAME,count(SC.c#) from sc,COURSE where SC.C#=COURSE.C# group by COURSE.CNAME;

43、查询每门功成绩最好的前两名

44、统计每门课程的学生选修人数,要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人    数相同,按课程号升序排列
select c#,count(c#) from sc group by c# order by count(c#) desc, c# asc;

45、检索至少选修两门课程的学生学号
select SC.S# from SC ,STUDENT where SC.S#=STUDENT.S#  group by SC.S#,SNAME  HAVING count(c#)>=2

46、查询全部学生都选修的课程的课程号和课程名
SELECT co.c#, co.cname
FROM sc ss
INNER JOIN course co ON co.c# = ss.c#
GROUP BY co.c#, co.cname
HAVING COUNT(co.c#) = (SELECT COUNT (s#) FROM student)

48、查询两门以上不及格课程的同学的学号及其平均成绩
select s#,count(s#),avg(score) from sc  where score<60 group by s# HAVING count(s#) >1
 

 

posted @ 2018-05-30 10:46  茹此这样  阅读(2935)  评论(0编辑  收藏