刚入职时候做的练习题,今天离职,怀念一下。
/*******初始化学生表的数据******/
create table student(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5)
);
insert into student values ('s001','趙三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','胡四',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王理',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','劉玉',21,'男');
insert into student values ('s008','肖蓉',21,'女');
insert into student values ('s009','陳小',23,'女');
insert into student values ('s010','陳美',22,'女');
commit;
/******************初始化教师表***********************/
create table teacher(
tno varchar2(10) primary key,
tname varchar2(20)
);
insert into teacher values ('t001', '劉章');
insert into teacher values ('t002', '明燕');
insert into teacher values ('t003', '胡明星');
commit;
/***************初始化课程表****************************/
create table course(
cno varchar2(10),
cname varchar2(20),
tno varchar2(20),
constraint pk_course primary key (cno,tno)
);
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');
commit;
/***************初始化成绩表***********************/
create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2),
constraint pk_sc primary key (sno,cno)
);
insert into sc values ('s001','c001',78.9); 78.9
insert into sc values ('s002','c001',80.9); 80.9
insert into sc values ('s003','c001',81.9); 81.9
insert into sc values ('s004','c001',60.9); 60.9
insert into sc values ('s001','c002',82.9); 82.9
insert into sc values ('s002','c002',72.9); 72.9
insert into sc values ('s003','c002',81.9); 81.9
insert into sc values ('s001','c003','59');
commit;
练习:
注意:以下练习中的数据是根据初始化到数据库中的数据来写的SQL 语句,请大家务必注意。
create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2),
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select s1.SCORE
from SC s1 join SC s2 on s1.SNO = s2.SNO
where s1.CNO='c001' and s2.CNO = 'c002' and s1.SCORE > s2.SCORE;
2、查询平均成绩大于60 分的同学的学号和平均成绩;
select SNO,AVG(SCORE) pingjun
from SC
group by SNO
having avg(SCORE)>60;
3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT S.SNO,ST.SNAME,COUNT(S.CNO) xuankeshum,SUM(S.SCORE) zongchengji
FROM STUDENT ST JOIN SC S on ST.SNO = S.SNO
GROUP BY S.SNO,ST.SNAME;
4、查询姓“刘”的老师的个数;
SELECT COUNT(*)
FROM TEACHER
where TNAME LIKE '%明%';
5、查询没学过“明燕”老师课的同学的学号、姓名;
SELECT ST.SNo,ST.SNAME
FROM STUDENT ST,SC S,(
SELECT c.CNO
FROM COURSE c , TEACHER t
where c.TNO = t.TNO and t.TNAME != '明燕')cn
where ST.SNO = S.SNO and S.CNO=cn.CNO;
6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
select DISTINCT st.SNO,st.SNAME,s.CNO
FROM STUDENT st,SC s,(select s1.CNO
from SC s1,STUDENT st1
where s1.SNO = st1.SNO)s2
where st.SNO = s.SNO and s.CNO='c002' and s2.CNO = 'c001' ;
7、查询学过“明燕”老师所教的所有课的同学的学号、姓名;
SELECT DISTINCT ST.SNO,ST.SNAME
FROM STUDENT ST,(
SELECT C.CNO
FROM TEACHER T,COURSE C
where T.TNO = C.TNO and T.TNAME='明燕')TC,SC s
where TC.CNO = s.CNO ORDER BY ST.SNO ;
8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
SELECT DISTINCT ST.SNO,ST.SNAME
FROM STUDENT ST,(
SELECT T.SCORE,T.SNO
FROM SC T
where T.CNO='c001')TC,SC s
where TC.SNO = s.SNO and ST.SNO=s.SNO and s.CNO = 'c002' AND TC.SCORE > s.SCORE ORDER BY ST.SNO ;
9、查询所有课程成绩小于60 分的同学的学号、姓名;
SELECT ST.SNO,ST.SNAME
FROM STUDENT ST,
(SELECT SNO,CNO,SCORE
FROM SC
WHERE SCORE<60)s
where ST.SNO=s.SNO;
10、查询没有学全所有课的同学的学号、姓名;
SELECT DISTINCT ST.SNO,ST.SNAME
FROM STUDENT ST
where (SELECT COUNT(CNAME) FROM COURSE )>
(SELECT COUNT(CNO) FROM SC s where s.SNO = ST.SNO);
11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
SELECT DISTINCT ST.SNO,ST.SNAME
FROM STUDENT ST,(SELECT CNO
FROM SC
WHERE SNO='s001')CN,SC s
where s.CNO =CN.CNO and ST.SNO = s.SNO and s.SNO != 's001';
12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;
13、把“SC”表中“明燕”老师教的课的成绩都更改为此课程的平均成绩;
update SC SET SC.SCORE=(SELECT SUM(s.SCORE)/ COUNT(s.SCORE) su
FROM SC s,(
SELECT T.TNAME,c.CNO,c.CNAME
FROM TEACHER T,COURSE c
where T.TNO = c.TNO and T.TNAME ='明燕')TC
where s.CNO = TC.CNO ) where SC.CNO in(SELECT c.CNO
FROM TEACHER T,COURSE c
where T.TNO = c.TNO and T.TNAME ='明燕') ;
14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT CN.SNO,ST.SNAME
FROM STUDENT ST,(SELECT S.SNo
FROM SC S
WHERE S.CNO IN (SELECT CNO FROM SC WHERE SNO ='s001') AND S.SNO<>'s001'
GROUP BY S.SNO
HAVING COUNT(1)=(SELECT COUNT(1) FROM SC WHERE SNO = 's001'))CN
WHERE CN.SNo = ST.SNO;
15、删除学习“明燕”老师课的SC 表记录;
delete from SC where CNO IN (SELECT CNO
FROM TEACHER,COURSE
WHERE TEACHER.TNO=COURSE.TNO and TNAME = '明燕');
16、向SC 表中插入一些记录,这些记录要求符合以下条件:
insert into sc
select sno,'c002',(select avg(score) from sc where cno='c002')
from student
where sno not in(select sno from sc where cno='c002');
17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT S.CNO,S.SCORE 最高,S1.SCORE 最低
FROM SC S,SC S1
WHERE S.CNO = S1.CNO
and S.SCORE = (SELECT MAX(S2.SCORE) FROM SC S2,STUDENT ST WHERE S2.CNO = S.CNO
AND ST.SNO = S2.SNO
GROUP BY S2.CNO)
and S1.SCORE = (SELECT MIN(Ss.SCORE) FROM SC Ss WHERE Ss.CNO = S1.CNO GROUP BY Ss.CNO);
18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
19、查询不同老师所教不同课程平均分从高到低显示
SELECT C.CNO,AVG(S.SCORE) 均分
FROM SC S,(SELECT T.TNAME,C.CNO,C.CNAME
FROM COURSE C,TEACHER T
WHERE T.TNO=C.TNO
ORDER BY T.TNAME) C
WHERE S.CNO = C.CNO
GROUP BY C.CNO;
20、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT S.CNO,S1.CNAME,
(SELECT COUNT(*) FROM SC c WHERE c.SCORE BETWEEN 100 AND 85 AND S.CNO = c.CNO)a ,
(SELECT COUNT(*) FROM SC c WHERE c.SCORE < 85 AND c.SCORE > 70 AND S.CNO = c.CNO)b,
(SELECT COUNT(*) FROM SC c WHERE c.SCORE < 70 AND c.SCORE > 60 AND S.CNO = c.CNO)c,
(SELECT COUNT(SCORE) FROM SC c WHERE c.SCORE < 60 AND S.CNO = c.CNO)d
FROM SC S,COURSE S1
WHERE S.CNO = S1.CNO
GROUP BY S.CNO,S1.CNAME;
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT S.SNO,S.CNO,S.SCORE
FROM SC S
WHERE S.SCORE IN (SELECT c.SCORE FROM SC c WHERE S.CNO = c.CNO ORDER BY SCORE DESC )
ORDER BY S.CNO;
22、查询每门课程被选修的学生数
SELECT C.CNO,C.CNAME,(SELECT COUNT(SNO) FROM SC S WHERE C.CNO = S.CNO )xuanxiushu
FROM COURSE C;
23、查询出只选修了一门课程的全部学生的学号和姓名
SELECT S.SNO,ST.SNAME
FROM SC S,STUDENT ST
WHERE S.SNO = ST.SNO
AND (SELECT COUNT(CNO) SC FROM SC WHERE SNO = S.SNO) = 1;
24、查询男生、女生人数
SELECT COUNT(SSEX) FROM STUDENT WHERE SSEX='男';
25、查询姓“张”的学生名单
SELECT * FROM STUDENT WHERE SNAME LIKE '張%';
26、查询同名同性学生名单,并统计同名人数
SELECT DISTINCT ST.SNAME,
(SELECT COUNT(SNAME)
FROM STUDENT SC
WHERE SC.SNAME = ST.SNAME AND SC.SNO <> ST.SNO )shumu
FROM STUDENT ST;
27、1981 年出生的学生名单(注:Student 表中Sage 列的类型是number)
SELECT SNO,SNAME,SAGE sa
FROM STUDENT
WHERE to_char(SYSDATE,'yyyy')-SAGE=1981;
28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT C.CNO,(SELECT AVG(S.SCORE) FROM SC S WHERE C.CNO = S.CNO)junfen
FROM COURSE C
ORDER BY junfen,C.CNO;
29、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
SELECT ST.SNO,ST.SNAME,
(SELECT AVG(S.SCORE) FROM SC S WHERE S.SNO = ST.SNO)AS junfen
FROM STUDENT ST
WHERE (SELECT AVG(S.SCORE) FROM SC S WHERE S.SNO = ST.SNO) >= 85;
30、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数
SELECT ST.SNAME,C.CNAME,S.SCORE
FROM STUDENT ST,SC S,COURSE C
WHERE ST.SNO = S.SNO
AND C.CNAME='PHP'
AND S.SCORE < 60;
31、查询所有学生的选课情况;
SELECT S.SNO,ST.SNAME,C.CNO,C.CNAME
FROM STUDENT ST,SC S,COURSE C
WHERE ST.SNO = S.SNO
AND S.CNO = C.CNO
ORDER BY S.SNO;
32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
SELECT DISTINCT ST.SNAME,C.CNAME,S.SCORE
FROM STUDENT ST,SC S,COURSE C
WHERE ST.SNO = S.SNO
AND C.CNO = S.CNO
AND S.SCORE >70
ORDER BY ST.SNAME;
33、查询不及格的课程,并按课程号从大到小排列
SELECT S.CNO
FROM SC S
WHERE S.SCORE <60
ORDER BY S.CNO DESC;
34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
SELECT S.SNO,ST.SNAME
FROM SC S,STUDENT ST
WHERE S.SNO = ST.SNO
AND S.CNO = 'c001'
AND S.SCORE >= 80;
35、求选了课程的学生人数
SELECT COUNT(*)
FROM (SELECT S.SNO,COUNT(*)
FROM SC S
GROUP BY S.SNO)s
;
36、查询选修“明燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT ST.SNAME,S.SCORE,T.TNAME,C.CNAME
FROM STUDENT ST,SC S,TEACHER T,COURSE C
WHERE ST.SNO = S.SNO
AND T.TNO = C.TNO
AND S.CNO = C.CNO
AND T.TNAME='明燕'
AND S.SCORE= (SELECT MAX(SCORE) FROM SC WHERE CNO = C.CNO);
37、查询各个课程及相应的选修人数
SELECT CNO,COUNT(*) FROM SC GROUP BY CNO;
38、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select s.sno,s.cno,s.score
from sc s
where s.score in (select score from sc where sno<>s.sno and cno<>s.cno);
39、查询每门功课成绩最好的前两名
select *
from (select sno,cno,score,
row_number()over(partition by cno,score order by sno)rn from SC)
where rn <2;
40、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno,count(sno)
from sc
group by cno
having count(sno)>10
order by 2 desc,cno asc;
41、检索至少选修两门课程的学生学号
select sno,count(cno)
from sc
group by sno
having count(cno)>1;
42、查询全部学生都选修的课程的课程号和课程名
select c.cno,c.cname
from course c
where c.cno in ( select s.cno
from sc s group by s.cno having count(*) =
(select count(*) from student) and s.cno = c.cno);
43、查询没学过“明燕”老师讲授的任一门课程的学生姓名
select sname
from student
where not exists (select DISTINCT s.sno,c.cno,c.cname from sc s,course c,teacher t
where t.tno = c.tno and c.CNO = s.cno and s.sno = sno and
t.tname = '明燕' );
44、查询两门以上不及格课程的同学的学号及其平均成绩
select s.sno,avg(s.score)
from SC s
where s.SCORE < 60
group by s.sno
having count(s.cno)>2;
45、检索“c004”课程分数小于60,按分数降序排列的同学学号
select sno
from sc
where cno='c004' and score < 60
order by score desc;
46、删除“s002”同学的“c001”课程的成绩
delete from SC sno='s002' and cno='c001';