Student(SID, Sname, Sage, Ssex) 学生表
Course(CID, Cname, TID) 课程表
SC(SID, CID, score) 成绩表
Teacher(TID, Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
SELECT * FROM SC SC1,SC SC2
WHERE SC1.CID='01' AND SC2.CID='02' AND SC1.SID=SC2.SID AND SC1.SCORE > SC2.SCORE;
2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT SC.SID,ROUND(AVG(NVL(SC.SCORE,0)),1) AVGSCORE FROM SC GROUP BY SC.SID HAVING AVG(NVL(SC.SCORE,0)) >= 60;
3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT S.SID,S.SNAME,COUNT(SC.CID) SOURCE_COUNT,SUM(SC.SCORE) SUM_SCORE FROM STUDENT S
LEFT JOIN SC ON S.SID=SC.SID
GROUP BY S.SID,S.SNAME;
4、查询姓“李”的老师的个数;
SELECT COUNT(1) FROM TEACHER T WHERE T.TNAME LIKE '李%'
5、查询没学过“叶平”老师课的同学的学号、姓名;
SELECT *FROM STUDENT WHERE SID NOT IN(
SELECT S.SID FROM STUDENT S
LEFT JOIN SC ON S.SID=SC.SID
LEFT JOIN COURSE C ON SC.CID = C.CID
LEFT JOIN TEACHER T ON T.TID=C.TID
WHERE T.TNAME='叶平'
)
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT * FROM STUDENT S, SC C1, SC C2 WHERE C1.CID='01' AND C2.CID='02'
AND S.SID = C1.SID AND S.SID = C2.SID
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT STU_COURSE.SID,STU_COURSE.SNAME,COUNT(STU_COURSE.CID) FROM
(SELECT S.SID,S.SNAME,SC.CID FROM STUDENT S LEFT JOIN SC ON S.SID = SC.SID WHERE SC.CID IN
(SELECT C.CID FROM COURSE C, TEACHER T WHERE C.TID=T.TID AND T.TNAME='叶平')) STU_COURSE
GROUP BY STU_COURSE.SID,STU_COURSE.SNAME HAVING COUNT(STU_COURSE.CID)=
(SELECT COUNT(C.CID) FROM COURSE C,TEACHER T WHERE C.TID=T.TID AND T.TNAME='叶平')
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT S.SID,S.SNAME FROM SC SC1 , SC SC2,STUDENT S
WHERE SC1.SID = SC2.SID
AND SC1.CID = '001'
AND SC2.CID = '002'
AND SC1.SCORE < SC2.SCORE
9、查询所有课程成绩小于60分的同学的学号、姓名;
SELECT DISTINCT SC1.SID,S.SNAME FROM SC SC1, STUDENT S WHERE S.SID NOT IN
(SELECT DISTINCT SC.SID FROM SC WHERE SC.SCORE >= 60) AND SC1.SID=S.SID
10、查询没有学全所有课的同学的学号、姓名;
SELECT S.SID,S.SNAME,COUNT(SC.CID) COURSE_COUNT
FROM STUDENT S LEFT JOIN SC ON S.SID=SC.SID GROUP BY S.SID,S.SNAME
HAVING COUNT(SC.CID)<(SELECT COUNT(1) FROM COURSE)
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
SELECT DISTINCT S.* FROM STUDENT S LEFT JOIN SC C ON S.SID=C.SID
WHERE C.CID IN
(SELECT SC.CID FROM SC WHERE SC.SID='01') AND S.SID <> '01'
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
SELECT S.SID,S.SNAME FROM STUDENT S
WHERE S.SID IN
(SELECT DISTINCT S.SID FROM SC WHERE SC.CID NOT IN (SELECT SC.CID FROM SC WHERE S.SID='01'))
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
update sc sc3
set sc3.score = (select round(avg(sc.score),2) avgscore from teacher t ,course c, sc where t.tid = c.cid and c.cid = sc.cid and t.tname = '叶平'
and c.cid = sc3.cid group by c.cid)
where sc3.cid in
(select c.cid from teacher t,course c,sc where t.tid=c.cid and c.cid=sc.cid and t.tname = '叶平'
group by c.cid)
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
select * from student s,(select sc4.sid,count(1) course_count from sc sc4 group by sc4.sid) a,
(select sc2.sid,count(sc2.cid) course_count from sc sc2 where sc2.cid in(
select * from sc sc1 where sc1.sid = '1002'
)group by sc2.sid having count(sc2.cid) < (select count(1) from sc sc3 where sc3.sid = '1002')) b
where a.sid = b.sid and a.course_count = b.course_count and s.sid = a.sid and s.sid <> '1002'
15、删除学习“叶平”老师课的SC表记录;
delete from sc
where sc.cid in
(select c.cid from teacher t,course c where t.tid = c.tid and t.tname = '叶平')
16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩;
SELECT '03',A.SID,B.AVGSCORE FROM(
SELECT * FROM STUDENT S WHERE S.SID NOT IN (SELECT SC.SID FROM SC WHERE SC.CID='03')) A LEFT JOIN(
SELECT ROUND(AVG(SC.SCORE),2) AVGSCORE FROM SC WHERE SC.CID = '02') B ON 1=1
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分
SELECT '03',A.SID,B.AVGSCORE FROM(
SELECT * FROM STUDENT S WHERE S.SID NOT IN (SELECT SC.SID FROM SC WHERE SC.CID='03')) A LEFT JOIN(
SELECT ROUND(AVG(SC.SCORE),2) AVGSCORE FROM SC WHERE SC.CID = '02') B ON 1=1
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT SC.CID,MIN(SC.SCORE) MINSCORE,MAX(SC.SCORE) MAXSCORE FROM SC GROUP BY SC.CID
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT SC.CID,ROUND(AVG(SC.SCORE),2) AVGSCORE,
ROUND((SELECT COUNT(SC1.CID) FROM SC SC1 WHERE SC1.CID = SC.CID AND SC1.SCORE>=60)/COUNT(SC.SID),2) JIGELV
FROM SC GROUP BY SC.CID ORDER BY AVGSCORE,JIGELV DESC
20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)
SELECT MAX(DECODE(V1.CID,'01',V1.CID || ' - ' || V1.AVGSCORE || ' - ' || V1.JIGELV)) "语文",
MAX(DECODE(V1.CID,'01',V1.CID || ' - ' || V1.AVGSCORE || ' - ' || V1.JIGELV)) "数学"
from v1
21、查询不同老师所教不同课程平均分从高到低显示
SELECT T.TID, C.CID, AVG(SC.SCORE) AVGSCORE FROM SC, COURSE C,TEACHER T
WHERE SC.CID = C.CID AND C.TID = T.TID
GROUP BY T.TID,C.CID ORDER BY AVGSCORE DESC
22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)
SELECT * FROM(
SELECT * FROM (
SELECT SC.CID,SC.SCORE "语文",ROWNUM R FROM SC WHERE SC.CID='01' AND ROWNUM<=6 ORDER BY SC.SCORE DESC) A
WHERE A.R>=3 ORDER BY A.R) SCORE1
UNION ALL
SELECT * FROM(
SELECT * FROM (
SELECT SC.CID,SC.SCORE "数学",ROWNUM R FROM SC WHERE SC.CID='02' AND ROWNUM<=6 ORDER BY SC.SCORE DESC) A
WHERE A.R>=3 ORDER BY A.R) SCORE2
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT C.CID,C.CNAME, COUNT(S.SID),
(SELECT COUNT(SC.SID) FROM SC WHERE SC.CID = C.CID AND SC.SCORE > 85) "85-100人数",
(SELECT COUNT(SC.SID) FROM SC WHERE SC.CID = C.CID AND SC.SCORE >= 60 AND SC.SCORE <= 85) "60-85人数"
FROM SC S,COURSE C WHERE S.CID = C.CID GROUP BY C.CID,C.CNAME
24、查询学生平均成绩及其名次
SELECT A.*,
( SELECT COUNT(1)
FROM (SELECT SC.SID,AVG(SC.SCORE) AVGSCORE FROM SC GROUP BY SC.SID)B WHERE B.AVGSCORE>A.AVGSCORE)+1 MC
FROM(
SELECT SC.SID,AVG(SC.SCORE) AVGSCORE FROM SC GROUP BY SC.SID) A ORDER BY MC
25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
select a.sid,a.cid,a.score from sc a
left join sc b
on a.cid = b.cid and a.score
having count(b.cid)❤️
order by a.cid;
26、查询每门课程被选修的学生数
select sc.cid,count(s.sid) "人数" from sc,student s
group by sc.cid
27、查询出只选修了一门课程的全部学生的学号和姓名
select sc.sid,s.sname,count(sc.cid) "选课数"
from sc, student s
where sc.sid = s.sid group by sc.sid,
s.sname having count(sc.cid)=1
28、查询男生、女生人数
SELECT COUNT(SSEX) AS 男生人数 FROM STUDENT S
GROUP BY SSEX HAVING SSEX='男';
SELECT COUNT(SSEX) AS 女生人数 FROM STUDENT S
GROUP BY SSEX HAVING SSEX='女';
29、查询姓“张”的学生名单
SELECT * FROM STUDENT S WHERE S.SNAME LIKE '张%'
30、查询同名同性学生名单,并统计同名人数
SELECT S.SNAME,COUNT(*) FROM STUDENT S GROUP BY S.SNAME HAVING COUNT(*)>1
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age
from student
where CONVERT(char(11),DATEPART(year,Sage))='1981';
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select sc.cid,avg(sc.score) "mean" from sc
group by sc.cid
order by avg(sc.score) asc,sc.cid desc
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select s.sname,sc.sid,avg(sc.score) from student s, sc
where s.sid = sc.sid group by sc.sid,s.sname having avg(sc.score)>85
34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
select s.sname,sc.score from student s, sc
where s.sid = sc.sid group by sc.sid,sc.score,s.sname having sc.score < 60
35、查询所有学生的选课情况;
select sc.sid,sc.cid,s.sname,c.cname
from sc,student s,course c
where sc.sid = s.sid and sc.cid=c.cid
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select s.sid,s.sname,sc.cid,sc.score
from student s,sc
where sc.score >=70 and sc.sid = s.sid
37、查询不及格的课程,并按课程号从大到小排列
select * from sc where sc.score<60 order by sc.cid
38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select s.sid,s.sname from sc,student s where sc.score>80
and sc.cid='003'
and sc.sid = s.sid
39、求选了课程的学生人数
select count(*) from sc
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select s.sname,sc.score
from student s,sc,course c,teacher t
where s.sid=sc.sid and sc.cid = c.cid
and c.tid = t.tid and t.tname = ‘叶平’ and sc.score = (select max(score)from sc where sc.cid = c.cid)
and rownum<=1;
41、查询各个课程及相应的选修人数
select count(*) from sc group by sc.cid
42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select distinct a.sid,b.score from sc a,sc b where a.score = b.score and a.cid <> b.cid
43、查询每门功成绩最好的前两名
select a.sid,a.cid,a.score from sc a
left join sc b
on a.cid = b.cid and a.score
having count(b.cid)<2
order by a.cid;
44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT sc.cid, COUNT( distinct sc.sid) as num FROM sc
GROUP BY sc.cid HAVING count(sc.sid)>10
ORDER BY num DESC,sc.cid ASC
45、检索至少选修两门课程的学生学号
select sc.sid from sc
group by sc.sid
having count(*) >=2
46、查询全部学生都选修的课程的课程号和课程名
select * from course c where c.cid in(
select sc.cid from sc group by sc.cid having count(sc.sid)=(select count(1) from student) )
47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
select * from student where sid not in (
select s.sid from student s
left join sc on s.sid = sc.sid
left join course c on sc.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = ‘叶平’)
48、查询两门以上不及格课程的同学的学号及其平均成绩
select c1.sid,avg(c1.score) from sc c1 where c1.sid in(
select sc.sid
from sc where sc.score < 60
group by sc.sid
having count(sc.cid) >= 2)
group by c1.sid