**
**
以下练习题是根据初始化到数据库中的表执行的,请务必先执行如下建表语句。
```sql
create table s1_student( sno varchar2(10) primary key, sname varchar2(30), sbirthday varchar2(30), ssex varchar2(10),sclass varchar2(10) );
create table s1_teacher( tno varchar2(10) primary key, tname varchar2(30),tsex varchar2(10),tbirthday varchar2(30),prof varchar2(30),depart varchar2(30) );
create table s1_course( cno varchar2(10), cname varchar2(30), tno varchar2(10) );
create table s1_score( sno varchar2(10), cno varchar2(10), degree number );
create table s1_grade( low number, upp number, rank varchar2(2) );
```sql
/*******初始化学生表s1_student******/
insert into s1_student values ('108', '曾华', '1997/9/1', '男', '95033');
insert into s1_student values ('105', '匡明', '1995/10/2', '男', '95031');
insert into s1_student values ('107', '王丽', '1996/1/23', '女', '95033');
insert into s1_student values ('101', '李军', '1996/2/20', '男', '95033');
insert into s1_student values ('109', '王芳', '1995/2/10', '女', '95031');
insert into s1_student values ('103', '陆君', '1994/6/3', '男', '95031');
commit;
/*******初始化教师表s1_teacher*******/
insert into s1_teacher values ('804', '李诚','男','1979-12-2','副教授','计算机系');
insert into s1_teacher values ('856', '张旭','男','1985-3-12','讲师','电子工程系');
insert into s1_teacher values ('825', '王萍','女','1989-5-2','助教','计算机系');
insert into s1_teacher values ('831', '刘冰','女','1988-8-2','助教','电子工程系');
commit;
/*******初始化课程表s1_course********/
insert into s1_course values ('3-105', '计算机导论', '825');
insert into s1_course values ('3-245', '操作系统', '804');
insert into s1_course values ('6-166', '数据电路', '856');
insert into s1_course values ('9-888', '高等数学', '800');
commit;
/********初始化成绩表s1_score*********/
insert into s1_score values ('103', '3-245', 86);
insert into s1_score values ('105', '3-245', 75);
insert into s1_score values ('109', '3-245', 68);
insert into s1_score values ('103', '3-105', 92);
insert into s1_score values ('105', '3-105', 88);
insert into s1_score values ('109', '3-105', 76);
insert into s1_score values ('101', '3-105', 64);
insert into s1_score values ('107', '3-105', 91);
insert into s1_score values ('108', '3-105', 78);
insert into s1_score values ('101', '6-166', 85);
insert into s1_score values ('107', '6-106', 79);
insert into s1_score values ('108', '6-166', 81);
commit;
/********初始化成绩分类表s1_grade*********/
insert into s1_grade values ('90', '100', 'A');
insert into s1_grade values ('80', '89', 'B');
insert into s1_grade values ('70', '79', 'C');
insert into s1_grade values ('60', '69', 'D');
insert into s1_grade values ('0', '59', 'E');
commit;
1.查询s1_student表中的所有记录的Sname、Ssex和Class列.
select sname,ssex,sclass from s1_student;
2.查询教师所有的单位即不重复的Depart列.
select distinct depart from s1_teacher;
3.查询s1_student表的所有记录.
select * from s1_student;
4.查询s1_score表中成绩在60到80之间的所有记录.
select * from s1_score where degree between 60 and 80;
5.查询s1_score表中成绩为85,86或88的记录.
select * from s1_score where degree in (85,86,88);
6.查询s1_student表中“95031”班或性别为“女”的同学记录.
select * from s1_student where sclass='95031' or ssex='女';
7.以Class降序查询s1_student表的所有记录.
select * from s1_student order by sclass desc;
8.以Cno升序、Degree降序查询s1_score表的所有记录。
select * from s1_score order by cno,degree desc;
9.查询“95031”班的学生人数。
select count(sno) from s1_student where sclass='95031';
10.查询s1_score表中的最高分的学生学号和课程号。
select sno,cno from s1_score where degree =(select max(degree) from s1_score);
11.查询‘3-105’号课程的平均分。
select avg(degree) from s1_score where cno='3-105';
12.查询s1_score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno,avg(degree) from s1_score where cno like '3%' group by cno
having count(sno)>5;
13.查询最低分大于70,最高分小于90的Sno列。
select sno from s1_score group by sno having min(degree)>70 and max(degree)<90;
14.查询所有学生的Sname、Cno和Degree列。
select sname,cno ,degree from s1_student st,s1_score sc where st.sno=sc.sno(+);
15.查询所有学生的Sno、Cname和Degree列。
select sno,cname,degree from s1_score sc,s1_course co where sc.cno(+)=co.cno;
16.查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from s1_student st,s1_score sc,s1_course co where st.sno=sc.sno(+) and sc.cno=co.cno(+);
17.查询“95033”班所选课程的平均分。
select avg(degree) from s1_score sc,s1_student st where st.sno=sc.sno and sclass='95033';
18.现查询所有同学的Sno、Cno和rank列。
select sno,cno,degree,(select rank from s1_grade where low<=degree and upp>=degree)as rank from s1_score;
19.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
(1)select * from s1_score where cno='3-105' and degree >(select degree from s1_score where cno='3-105' and sno='109');
(2)select x.CNO ,x.SNO ,x.DEGREE from s1_score x,s1_score y
where x.CNO ='3-105' and x.DEGREE >y.DEGREE and y.SNO ='109'and y.CNO='3-105';
20.查询s1_score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select * from s1_score j where j.SNO in (
select SNO from s1_score group by SNO having count(*)>=2) and
j.DEGREE <(select max(DEGREE)from s1_score b where b.CNO = j.CNO );
21.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from s1_score where degree>(select degree from s1_score where sno='109'and cno='3-105') and cno='3-105';
22.查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from s1_student st1 where substr(sbirthday,1,4)in (select substr(sbirthday,1,4) from s1_student where sno='101');
23.查询“张旭“教师任课的学生成绩。
select sc.* from s1_score sc,s1_teacher th,s1_course co where sc.cno=co.cno and co.tno=th.tno and th.tname='张旭';
24.查询选修某课程的同学人数多于5人的教师姓名。
(1)select tname from s1_teacher th,s1_course co where th.tno=co.tno and co.cno in (select cno from s1_score group by cno having count(sno)>5);
(2)select TNAME from s1_teacher where TNO in(
select x.TNO from s1_course x inner join s1_score y on x.CNO =y.CNO
group by x.TNO having count(x.TNO )>5);
25.查询95033班和95031班全体学生的记录。
select * from s1_student where sclass in('95033','95031');
26.查询存在有85分以上成绩的课程Cno.
select cno from s1_score group by cno having max(degree)>=85;
27.查询出“计算机系“教师所教课程的成绩表。
select sc.* from s1_score sc,s1_teacher th,s1_course co where sc.cno=co.cno and co.tno=th.tno and th.depart='计算机系';
28.查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select TNAME,PROF from s1_teacher where DEPART ='计算机系' and PROF not in(
select PROF from s1_teacher where DEPART ='电子工程系');
29.查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
(1)select sno,cno,degree from s1_score where cno='3-105' and degree >(
select min(degree) from s1_score where cno='3-245')order by degree desc;
(2)select * from s1_score where CNO ='3-105' and DEGREE >any (
select DEGREE from s1_score where CNO ='3-245')order by DEGREE desc;
30.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
(1)select sno,cno,degree from s1_score where cno='3-105' and degree >(
select max(degree) from s1_score where cno='3-245')order by degree desc;
(2)select * from s1_score where CNO ='3-105' and DEGREE >all(
select DEGREE from s1_score where CNO ='3-245');
31.查询所有教师和同学的name、sex和birthday.
select sname as name,ssex as sex from s1_student
union all
select tname as name,tsex as sex from s1_teacher;
32.查询所有“女”教师和“女”同学的name、sex和birthday.
select sname as name,ssex as sex,sbirthday as birthday from s1_student where ssex='女'
union all
select tname as name,tsex as sex,tbirthday as birthday from s1_teacher where tsex='女';
33.查询成绩比该课程平均成绩低的同学的成绩表。
(1)select sc1.* from s1_score sc1,(select cno,avg(degree)as degree from s1_score group by cno) sc2 where sc1.cno=sc2.cno and sc1.degree<sc2.degree;
(2)select * from s1_score a where DEGREE <(
select avg(DEGREE) from s1_score b where a.CNO=b.CNO);
34.查询所有任课教师的Tname和Depart.
(1)select tname,depart from s1_teacher th,s1_course co where th.tno=co.tno;
(2)select TNAME,DEPART from s1_teacher where exists
(select * from s1_course where s1_course.TNO = s1_teacher.TNO );
35.查询所有未讲课的教师的Tname和Depart.
(1)select tname,depart from s1_teacher th where th.tno not in (
select tno from s1_course);
(2)select tname,depart from s1_teacher th where not exists (
select tno from s1_course co where th.tno=co.tno);
36.查询至少有2名男生的班号。
select sclass from s1_student where ssex='男' group by sclass having count(sno)>=2;
37.查询s1_student表中不姓“王”的同学记录。
select * from s1_student where sname not like '王%';
38.查询s1_student表中每个学生的姓名和年龄。
select sname,to_char(sysdate,'yyyy')-substr(sbirthday,1,4) as tage from s1_student;
39.查询s1_student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday) from s1_student;
40.以班号和年龄从大到小的顺序查询s1_student表中的全部记录。
select * from s1_student order by sclass desc,sbirthday asc;
41.查询“男”教师及其所上的课程。
select th.tno,th.tname,th.tsex,co.cno from s1_teacher th,s1_course co where th.tno=co.tno and th.tsex='男';
42.查询最高分同学的Sno、Cno和Degree列。
select * from s1_score where degree =(select max(degree) from s1_score);
43.查询和“李军”同性别的所有同学的Sname.
select sname from s1_student where ssex =(select ssex from s1_student where sname='李军') and sname<>'李军';
44.查询和“李军”同性别并同班的同学Sname.
(1)select sname from s1_student st1 where exists
(select * from s1_student st2 where st2.sname='李军' and st1.sclass=st2.sclass and st1.ssex=st2.ssex);
(2)select SNAME from s1_student where SSEX =(select SSEX from s1_student where SNAME ='李军') and SCLASS =(select SCLASS from s1_student where SNAME ='李军');
45.查询所有选修“计算机导论”课程的“男”同学的成绩表
(1)select sc.* from s1_score sc,s1_course co,s1_student st
where sc.sno=st.sno and sc.cno=co.cno and co.cname='计算机导论' and st.ssex='男';
(2)select * from s1_score where SNO in(select SNO from s1_STUDENT where SSEX ='男')
and CNO =(select CNO from s1_COURSE where CNAME ='计算机导论');
**
**
以下练习题是根据初始化到数据库中的表执行的,请务必先执行如下建表语句。 (PS:第一阶段题目表和第二阶段题目表不同)
create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar2(10) );
create table teacher( tno varchar2(10) primary key, tname varchar2(20) );
create table course( cno varchar2(10), cname varchar2(20), tno varchar2(20), constraint pk_course primary key (cno,tno) );
create table sc( sno varchar2(10), cno varchar2(10), score number(4,2), constraint pk_sc primary key (sno,cno) );
/*******初始化学生表student******/
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;
/*******初始化教师表teacher*******/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '大佬');
insert into teacher values ('t003', '胡明星');
commit;
/*******初始化课程表course********/
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;
/********初始化成绩表sc*********/
insert into sc values ('s001', 'c001', 78.9);
insert into sc values ('s002', 'c001', 80.9);
insert into sc values ('s003', 'c001', 81.9);
insert into sc values ('s004', 'c001', 60.9);
insert into sc values ('s001', 'c002', 82.9);
insert into sc values ('s002', 'c002', 72.9);
insert into sc values ('s003', 'c002', 81.9);
insert into sc values ('s001', 'c003', '59');
commit;
1.查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select sc1.sno from sc sc1,sc sc2 where sc1.sno=sc2.sno and sc1.cno='c001' and sc2.cno='c002' and sc1.score>sc2.score;
2.查询平均成绩大于60 分的同学的学号和平均成绩;
select sno,avg(score) from sc group by sno having avg(score)>60;
3.查询所有同学的学号、姓名、选课数、总成绩;
select sc.sno,sname,count(cno)as cnt,sum(score)as score from sc, student st where sc.sno=st.sno group by sc.sno,sname;
4.查询姓“刘”的老师的个数;
select count(tno) from teacher where tname like '刘%';
5.查询没学过“大佬”老师课的同学的学号、姓名;
select sno,sname from student st where sno not in (select sno
from sc,course co,teacher th where sc.cno=co.cno and co.tno=th.tno and th.tname='大佬');
6.查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
(1)select sc.sno,sname from sc,student st where sc.sno=st.sno and cno ='c001' and sc.sno in(select sno from sc where cno='c002');
(2)select st.* from sc a join sc b on a.sno=b.sno join student st on st.sno=a.sno
where a.cno='c001' and b.cno='c002' and st.sno=a.sno;
7.查询学过“大佬”老师所教的所有课的同学的学号、姓名;
select s.sno,d.sname,count(distinct s.cno) from SC s,Student d,Course c,Teacher t
where s.sno=d.sno and s.cno=c.cno and c.tno=t.tno and t.tname='大佬'
group by s.sno,d.sname having count(distinct s.cno) =
(select count(*) from Course c,Teacher t where c.tno=t.tno and t.tname='大佬');
8.查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
select sc1.sno,sname from sc sc1,student st,sc sc2 where sc1.sno=sc2.sno and sc1.score>sc2.score and sc1.cno='c001' and sc2.cno='c002' and sc1.sno=st.sno;
9.查询所有课程成绩小于60 分的同学的学号、姓名;
select sc.sno,sname from sc,student st where sc.sno=st.sno and score <60;
10.查询没有学全所有课的同学的学号、姓名;
(1)select st.sno,st.sname,count(sc.sno) from sc,student st where st.sno=sc.sno(+)
group by st.sno,st.sname having count(sc.sno)<(select count(cno) from course);
(2)select * from student where sno in (select sno from
(select stu.sno,c.cno from student stu cross join course c
minus
select sno,cno from sc));
11.查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
select distinct st.sno,st.sname from sc,student st
where sc.sno=st.sno and sc.cno in (select cno from sc where sno='s001') and sc.sno<>'s001';
12.查询至少学过学号为“s002”同学所有门课的其他同学学号和姓名;
SELECT sno, Sname FROM student WHERE sno IN (SELECT sno,cno FROM SC WHERE cno IN (SELECT cno FROM SC WHERE sno = 's002') AND sno <> 's002' HAVING COUNT(cno) >= (SELECT COUNT(cno) FROM SC WHERE sno = 's002') GROUP BY sno);
13.把“SC”表中“大佬”老师教的课的成绩都更改为此课程的平均成绩;
update sc sc1
set score =( select avg(score) from sc sc2 where sc1.cno=sc2.cno group by sc2.cno )
where cno in ( select distinct co.cno from course co,teacher th
where sc1.cno=co.cno and co.tno=th.tno and th.tname='大佬');
commit;
14.查询和“s002”号的同学学习的课程完全相同的其他同学学号和姓名;
select distinct sno from sc where sno<>'s002' and sno not in
(select sno from sc where cno not in (select cno from sc where sno='s002'))
group by sno having count(*) = (select count(*) from sc where sno='s002');
15.删除学习“大佬”老师课的SC 表记录;
delete from sc where cno in (select distinct co.cno from course co,teacher th
where sc.cno=co.cno and co.tno=th.tno and th.tname='大佬');
16.向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
insert into sc (sno,cno,score)
select sno,'c002' as cno, (select avg(score) from sc where cno='c002')as score
from student where sno not in (select sno from sc where cno='c002');
commit;
17.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select cno,max(score),min(score) from sc group by cno;
18.按各科平均成绩从低到高和及格率的百分数从高到低顺序;
select cno,avg(score),sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率
from sc group by cno order by avg(score),及格率 desc ;
19.查询不同老师所教不同课程平均分从高到低显示;
select co.tno,sc.cno,avg(sc.score) from sc,course co where sc.cno=co.cno group by co.tno,sc.cno;
20.统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60];
select sc.cno,c.cname,
sum(case when score between 85 and 100 then 1 else 0 end) AS "[100-85]",
sum(case when score between 70 and 85 then 1 else 0 end) AS "[85-70]",
sum(case when score between 60 and 70 then 1 else 0 end) AS "[70-60]",
sum(case when score <60 then 1 else 0 end) AS "[<60]"
from sc, course c where sc.cno=c.cno group by sc.cno ,c.cname;
21.查询各科成绩前三名的记录:(不考虑成绩并列情况);
select * from (select cno,sno, row_number() over (partition by cno order by score) as rm from sc ) where rm<=3;
22.查询每门课程被选修的学生数;
select cno,count(sno) from sc group by cno;
23.查询出只选修了一门课程的全部学生的学号和姓名;
select sc.sno,sname from sc,student st where sc.sno=st.sno group by sc.sno,sname having count(*)=1;
24.查询男生、女生人数;
select ssex,count(sno) from student group by ssex;
25.查询姓“张”的学生名单;
select * from student where sname like ‘张%’;
26.查询同名同性学生名单,并统计同名人数;
select sname,count(sno)from student group by sname having count(*)>1;
27.1991 年出生的学生名单(注:Student 表中Sage 列的类型是number);
select * from student where sage=to_char(sysdate,‘yyyy’)-1991;
28.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select cno,avg(score) from sc group by cno order by avg(score),cno desc;
29.查询平均成绩大于85 的所有学生的学号、姓名和平均成绩;
select sc.sno,sname,avg(score) from sc,student st where sc.sno=st.sno group by sc.sno,sname having avg(score)>60;
30.查询课程名称为“数据库”,且分数低于60 的学生姓名和分数;
select sname,score from student st,sc,course co where sc.sno=st.sno and sc.cno=co.cno and cname='Java Web' and score>60;
31.查询所有学生的选课情况;
select sno,cno from sc;
32.查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
select sname,cname,score from student st,sc,course co where sc.sno=st.sno and sc.cno=co.cno and score>70;
33.查询不及格的课程,并按课程号从大到小排列;
select sc.cno,cname,score from sc,course co where sc.cno=co.cno and score<70 order by sc.cno desc;
34.查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
select sc.sno,sname,sc.score from student st,sc where sc.sno=st.sno and score>80 and cno='c001';
35.求选了课程的学生人数;
select count(distinct sno) from sc ;
36.查询选修“大佬”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
select st.sname,score from student st,sc ,course c,teacher t where
st.sno=sc.sno and sc.cno=c.cno and c.tno=t.tno and t.tname='大佬' and score =
(select max(score) from sc,course co,teacher th where sc.cno=co.cno and co.tno=th.tno and th.tname='大佬');
37.查询各个课程及相应的选修人数;
select cno,count(sno) from sc group by cno ;
38.查询不同课程成绩相同的学生的学号、课程号、学生成绩;
select sc1.sno,sc1.cno,sc1.score from sc sc1,sc sc2 where sc1.score=sc2.score and sc1.cno<>sc2.cno ;
39.查询每门功课成绩最好的前两名;
select * from (select sno,cno,score,row_number() over(partition by cno order by score) as rm from sc ) where rm<=2;
40.统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列;
select cno,count(sno) from sc group by cno having count(sno)>=10 order by count(sno) desc,cno asc;
41.检索至少选修两门课程的学生学号;
select sno from sc group by sno having count(cno)>=2;
42.查询全部学生都选修的课程的课程号和课程名;
select sc.cno,cname from sc,course co where sc.cno=co.cno
group by sc.cno,cname having count(sno)=(select count(distinct sno) from student);
43.查询没学过“大佬”老师讲授的任一门课程的学生姓名;
select st.sno,st.sname from student st where st.sno not in
(select distinct sc.sno from sc,course c,teacher t where sc.cno=c.cno and c.tno=t.tno and t.tname='大佬');
44.查询两门以上不及格课程的同学的学号及其平均成绩;
select sno,avg(score) from sc where sno in
(select sno from sc where score<90 group by sno having count(cno)>1) group by sno;
45.检索“c004”课程分数小于60,按分数降序排列的同学学号;
select sno,score from sc where cno='c002' and score <80 order by score desc;
46.删除“s002”同学的“c001”课程的成绩;
delete from sc where sno='s002' and cno='c001';