king

Oracle SQL题目及其解答(学生、课程、成绩、教师)

king PHP 2023-02-27 1010浏览 0

由于原文题目中的数据在我自己建的DB里没有数据,所以解答中的条件可能是符合我DB的条件,而不是符合题目的条件。但解答应该符合题目的意思要求。但是文中的解答肯能会有错误,但都是经过我DB中实际跑过的。欢迎指出错误。

select * from t_student 学生表

Oracle SQL题目及其解答(学生、课程、成绩、教师)

select * from t_course 课程表

Oracle SQL题目及其解答(学生、课程、成绩、教师)

select * from t_sc 学生成绩表

Oracle SQL题目及其解答(学生、课程、成绩、教师)

select * from t_teacher 老师表

Oracle SQL题目及其解答(学生、课程、成绩、教师)

–查询“1”课程比“2”课程成绩高的所有学生的学号
select a.stuid
from (select * from t_sc s where s.cid = 1) a,
(select * from t_sc s where s.cid = 2) b
where a.stuid = b.stuid
and a.score > b.score;
/*
select a.S#
from (select s#, score from SC where C# = ‘001’) a,
(select s#, score from SC where C# = ‘002’)
where a.score > b.score
and a.s# = b.s#;
*/
–2、查询平均成绩大于80分的同学的学号和平均成绩;
select stuid, avg(score) from t_sc group by stuid having avg(score)>80;

–3、查询所有同学的学号、姓名、选课数、总成绩;
select s.stuid, s.name, c.xks, c.zcj
from t_student s left outer join
(select stuid, count(cid) xks, sum(score) zcj
from t_sc
group by stuid) c
on s.stuid = c.stuid;

select s.stuid, s.name, count(c.cid), sum(c.score)
from t_student s
left Outer join t_sc c
on s.stuid = c.stuid
group by s.stuid, s.name;
–4、查询姓“李”的老师的个数;
select count(tid) from t_teacher where tname like ‘李%’;

–5、查询没学过“叶平”老师课的同学的学号、姓名;
select s.stuid,s.name from t_student s, t_course c, t_teacher t, t_sc sc where s.stuid=sc.stuid and c.cid=sc.cid and c.teacherid=t.tid and t.tname!=’叶平’;

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# andTeacher.Tname = ‘叶平’);

–6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select s.stuid,s.name from t_student s, t_sc c where c.cid=1 and s.stuid=c.stuid and exists (select * from t_sc sc where sc.stuid=s.stuid and sc.cid=2);

— select Student.S#,Student.Sname fromStudent,SC where Student.S#=SC.S# andSC.C#=’001’and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#=’002′);

–7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select s.stuid,s.name from t_student s, t_sc sc where s.stuid = sc.stuid and sc.cid in(
select c.cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname=’Mr.kay’);

select S#, Sname
from Student
where S# in
(select S#
from SC, Course, Teacher
where SC.C# = Course.C# andTeacher.T# = Course.T#
and Teacher.Tname = ‘叶平’
group by S#
having count(SC.C#) = (select count(C#) from Course,
Teacher where Teacher.T# = Course.T# and Tname = ‘叶平’));

–8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
Select S#, Sname
from (select Student.S#,
Student.Sname,
score,
(select score
from SC SC_2
where SC_2.S# = Student.S#and SC_2.C# = ‘002’) score2
from Student, SC
where Student.S# = SC.S# andC# = ‘001’) S_2
where score2 < score;

–9、查询所有课程成绩小于60分的同学的学号、姓名;
select s.stuid, s.name from t_student s where s.stuid not in (select sc.stuid from t_student s, t_sc sc where sc.score>=60 and s.stuid=sc.stuid);
select s.stuid, s.name from t_student s where not exists (select * from t_sc sc where sc.stuid=s.stuid and sc.score>=60);

–10、查询没有学全所有课的同学的学号、姓名;
select s.stuid, s.name from t_student s,t_sc sc where s.stuid=sc.stuid group by s.stuid,s.name having count(sc.cid) != (select count(cid) from t_course);

select Student.S#, Student.Sname
from Student, SC whereStudent.S# = SC.S#
group by Student.S#, Student.Sname
having count(C#) < (select count(C#) from Course);

–11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select distinct s.stuid,s.name from t_student s, t_sc sc where s.stuid=sc.stuid and s.stuid!=1 and sc.cid in (select cid from t_sc where stuid=1)
–select s.stuid,s.name from t_Student s, t_SC sc where s.stuid=SC.Stuid and sc.cid in (select Cid from t_SC where stuid=’1′);

–13、把“t_SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
update t_sc sc1 set sc1.score =
(select avg(score) from t_sc group by cid having cid in (select cid from t_teacher t, t_course c where t.tid=c.teacherid and t.tname=’Mr.mao’) and cid=sc1.cid);
/*
update t_sc sc_1
set sc_1.score =
(select avg(sc_2.score) from t_sc sc_2 where sc_2.cid = sc_1.cid)
from t_course,
t_teacher
where t_course.Cid = t_sc.cid and t_course.tid = t_teacher.tid
and t_teacher.tname = ‘Mr.mao’);
*/
select * from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid and t.tname=’Mr.mao’

–14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
select * from t_sc where stuid in(
select stuid
from t_sc
group by stuid
having count(*) = (select count(*) from t_sc where stuid = 6)
)
and cid in (select cid from t_sc where stuid=6);

–15、删除学习“叶平”老师课的SC表记录
delete t_sc where cid in(
select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname=’Mr.mao’);

–16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“5”课程的同学学号、2号课的平均成绩;
insert into t_sc
select stuid, ‘5’ cid, (select avg(score) from t_sc sc where sc.cid=2) score
from t_student s where not exists
(select * from t_sc sc where sc.cid=5 and sc.stuid=s.stuid);

–17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按
–如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

select t.stuid,
max(case t.cname when ‘Chinese’ then t.score end) Chinese,
max(case t.cname when ‘Math’ then t.score end) Math,
max(case t.cname when ‘English’ then t.score end) English,
round(avg(t.score), 2) avgscore,
count(t.cid) kcs
from (select s.stuid, s.name stuname, sc.cid, sc.score, c.name cname
from t_student s, t_sc sc, t_course c
where s.stuid = sc.stuid
and sc.cid = c.cid
and c.name in (‘Chinese’, ‘Math’, ‘English’)) t
group by t.stuid
order by avgscore desc;
/*
select sc.stuid,
(select score from t_sc where cid=1 and stuid=sc.stuid) Chinese,
(select score from t_sc where cid=2 and stuid=sc.stuid) Math,
(select score from t_sc where cid=3 and stuid=sc.stuid) English,
round(avg(sc.score),2) avgscore,
count(c.cid) kcs
from t_sc sc, t_course c where sc.cid=c.cid and c.name in (‘Chinese’, ‘Math’, ‘English’)
group by sc.stuid
order by avgscore desc;
*/

–18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select cid, max(score) maxscore, min(score) minscore from t_sc group by cid order by cid;
/*
SELECT L.Cid As 课程ID, L.score AS 最高分, R.score AS 最低分
FROM t_SC L, t_SC R
WHERE L.Cid = R.Cid
and L.score = (SELECT MAX(IL.score)
FROM t_SC IL, t_Student IM
WHERE L.Cid = IL.Cid
and IM.Stuid = IL.Stuid
GROUP BY IL.Cid)
AND R.Score =
(SELECT MIN(IR.score) FROM t_SC IR WHERE R.Cid = IR.Cid GROUP BY IR.Cid);
*/

–19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select sc.cid, (select name from t_course where cid=sc.cid) cname,
round(avg(sc.score),2) avgscore,
round((select count(*) from t_sc where cid = sc.cid and score>=70)/
(select count(*) from t_sc where cid = sc.cid)*100,2)||’%’ jgl
from t_sc sc
group by sc.cid
order by avgscore, jgl desc;
/*
SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG(score),0) AS平均成绩
,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
FROM SC T,Course
where t.C#=course.C#
GROUP BY t.C#
ORDER BY 100* SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
*/

–20、查询如下课程平均成绩和及格率的百分数(用”1行”显示):
–企业管理(001),马克思(002),OO&UML (003),数据库(004)
select * from t_sc sc group by sc.cid

select sc.cid, (select name from t_course where cid=sc.cid) cname,
case sc.cid when 1 then round(avg(sc.score),2) end chinese_avgscore,
case sc.cid when 1 then
round((select count(*) from t_sc where cid = sc.cid and score>=70)/
(select count(*) from t_sc where cid = sc.cid)*100,2)||’%’ end chinese_jgl
from t_sc sc
group by sc.cid

–21、查询不同老师所教不同课程平均分从高到低显示
select sc.cid,
round(avg(sc.score),2) avg_score,
c.name,
t.tname
from t_sc sc, t_course c, t_teacher t
where sc.cid=c.cid and t.tid=c.teacherid
group by sc.cid, c.name, t.tname
order by avg_score desc;

–22、查询如下语文成绩第 2 名到第 5 名的学生成绩单:
–[学生ID],[学生姓名],语文成绩, 排名
select * from (
select t.*, rownum tn from (
select s.stuid, s.name,
min(case c.name when ‘Chinese’ then sc.score end) chinese_score
from t_student s, t_sc sc, t_course c
where s.stuid = sc.stuid
and sc.cid = c.cid
group by s.stuid, s.name
order by chinese_score desc) t where rownum <=5) where tn>1

–23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select sc.cid, c.name,
(select count(*) from t_sc where cid=sc.cid and score>=85 and score<=100) a,
(select count(*) from t_sc where cid=sc.cid and score>=70 and score<85) b,
(select count(*) from t_sc where cid=sc.cid and score>=60 and score<70) c,
(select count(*) from t_sc where cid=sc.cid and score<60) d
from t_sc sc, t_course c
where sc.cid = c.cid
group by sc.cid, c.name;
/*
SELECT SC.Cid as 课程ID, c.name as 课程名称
,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS a
,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS b
,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS c
,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS d
FROM t_SC sc,t_Course c
where SC.Cid=C.Cid
GROUP BY SC.Cid,c.name;
*/

–24、查询学生平均成绩及其名次
select t.*, rownum rn from (
select s.stuid, s.name, avg(sc.score) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid
group by s.stuid, s.name
order by avg_score desc) t;

select t.*, row_number() over(order by avg_score desc) pm from (
select s.stuid, s.name, avg(sc.score) avg_score
from t_student s, t_sc sc where s.stuid=sc.stuid
group by s.stuid, s.name
) t;

–25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
select * from (
select sc.cid, c.name, sc.score, row_number() over(partition by sc.cid,c.name order by sc.score desc) pm
from t_sc sc, t_course c where sc.cid=c.cid ) where pm <=3;

/*
SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
FROM SC t1
WHERE score IN (SELECT TOP 3 score
FROM SC
WHERE t1.C#= C#
ORDER BY score DESC
)
ORDER BY t1.C#;
*/

–26、查询每门课程被选修的学生数
select cid, count(*) rs, (select name from t_course where cid=t_sc.cid) cname from t_sc group by cid;

–27、查询出只选修了3门课程的全部学生的学号和姓名
select sc.stuid, s.name from t_sc sc, t_student s where sc.stuid = s.stuid group by sc.stuid,s.name having count(*) =3;

–28、查询男生、女生人数
select case when sex=’1′ then ‘Man’ else ‘Woman’ end sex, count(*) rs from t_student group by sex;

–29、查询姓“张”的学生名单
select * from t_student where name like ‘w%’;

–30、查询同名同性学生名单,并统计同名人数
select name, count(*) rs from t_student group by name having count(*)>1;

–31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select to_char(sysdate,’yyyy’) year from dual;

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

–33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select s.stuid, s.name, round(avg(sc.score), 2) avg_score from t_student s, t_sc sc where s.stuid=sc.stuid group by s.stuid, s.name having avg(sc.score)>80;

–34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
select s.stuid, s.name stuname, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and c.name=’Computer’ and sc.score<70;

–35、查询所有学生的选课情况
select s.stuid, s.name stuname, c.cid, c.name coursename from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid;

–36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select s.stuid, s.name stuname, c.cid, c.name coursename, sc.score from t_student s, t_sc sc, t_course c where s.stuid=sc.stuid and sc.cid=c.cid and sc.score>70;

–37、查询不及格的课程,并按课程号从大到小排列
select c.cid, c.name from t_sc sc, t_course c where sc.cid=c.cid group by c.cid,c.name,sc.score having sc.score<70 order by c.cid desc;

–38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select s.stuid, s.name, sc.score from t_student s,t_sc sc where s.stuid=sc.stuid and sc.cid=3 and sc.score>80;

–39、求选了课程的学生人数
select count(*) rs from (select stuid from t_sc group by stuid);

–40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select s.stuid, s.name stuname, t.max_score from t_student s, t_sc sc,
(select c.cid, max(sc.score) max_score from t_sc sc, t_course c, t_teacher t where sc.cid=c.cid and c.teacherid=t.tid
and t.tname=’Mr.mao’ group by c.cid) t
where s.stuid=sc.stuid and sc.cid=t.cid and sc.score=t.max_score;
/*
select S.name stuname, sc.score
from t_Student s, t_SC sc, t_Course C, t_Teacher t
where S.Stuid = SC.Stuid
and SC.Cid = C.Cid
and C.Teacherid = T.tid
and T.Tname = ‘Mr.mao’
and SC.score = (select max(score) from t_SC where Cid = C.Cid);*/

–41、查询各个课程及相应的选修人数
select cid, count(*) rs from t_sc group by cid order by cid;

–42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select * from t_sc sc where exists (select * from t_sc where stuid!=sc.stuid and cid!=sc.cid and score=sc.score);
select distinct A.Stuid,B.score from t_SC A ,t_SC B where A.Score=B.Score and A.Cid <>B.Cid ;

–43、查询每门功成绩最好的前两名
select * from (select stuid, cid ,score, row_number() over(partition by cid order by score desc) pm from t_sc) where pm<3;
/*
SELECT t1.S# as 学生ID, t1.C# as 课程ID, Score as 分数
FROM SC t1
WHERE score IN
(SELECT TOP 2 score FROM SC WHERE t1.C# = C# ORDER BY score DESC)
ORDER BY t1.C#;
*/

–44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,
–查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid, count(*) rs from t_sc group by cid having count(*) > 5 order by rs desc, cid;

–45、检索至少选修两门课程的学生学号
select stuid ,count(*) from t_sc group by stuid having count(*) > 2;

–46、查询全部学生都选修的课程的课程号和课程名
select c.cid, c.name from t_course c where exists (
select cid, count(*) rs from t_sc where cid=c.cid group by cid having count(*) = (select count(*) from t_student)
);

–47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
select * from t_student where stuid not in(
select s.stuid from t_student s, t_sc sc where s.stuid=sc.stuid and sc.cid in (select cid from t_course c, t_teacher t where c.teacherid=t.tid and t.tname=’Mr.zhu’));

select * from t_Student s where s.stuid not in (select sc.stuid from t_Course c,t_Teacher t,t_SC sc where c.teacherid=T.Tid and SC.Cid=c.Cid and t.Tname=’Mr.zhu’);

–48、查询1门以上不及格课程的同学的学号及其平均成绩
select stuid, avg(score) avg_score from t_sc group by stuid having stuid in(
select stuid from t_sc where score<70 group by stuid having count(*) >1);

select S#,avg(isnull(score,0)) from SC where S# in (select S# from SC where score <60 group by S# having count(*)>1)group by S#;

–49、检索“5”课程分数小于60,按分数降序排列的同学学号
select stuid from t_sc where cid=5 and score<70 order by score desc;

–50、删除“002”同学的“001”课程的成绩
delete from t_sc where stuid=2 and cid=1;

继续浏览有关 数据库技术文章/教程 的文章
发表评论