oracle数据库网上经典45道练习题及答案写法

oracle数据库网上经典45道练习题及答案写法(有删减)
/*CREATE TABLE STUDENT1

(SNO VARCHAR(3) NOT NULL,

SNAME VARCHAR(4) NOT NULL,

SSEX VARCHAR(2) NOT NULL,

SBIRTHDAY DATE,

CLASS NUMBER NOT NULL);

CREATE TABLE COURSE1

(CNO VARCHAR(5) NOT NULL,

CNAME VARCHAR(10) NOT NULL,

TNO VARCHAR(10) NOT NULL);

CREATE TABLE SCORE1

(SNO VARCHAR(3) NOT NULL,

CNO VARCHAR(5) NOT NULL,

DEGREE NUMBER NOT NULL);

CREATE TABLE TEACHER1

(TNO VARCHAR(3) NOT NULL,

TNAME VARCHAR(4) NOT NULL,

TSEX VARCHAR(2) NOT NULL,

TBIRTHDAY DATE NOT NULL,

PROF VARCHAR(6),

DEPART VARCHAR(10) NOT NULL);*/

/*INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,‘曾华’ ,‘男’ ,to_date(‘1977-09-01’,‘yyyy-mm-dd’),95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,‘匡明’ ,‘男’ ,to_date(‘1975-10-02’,‘yyyy-mm-dd’),95031);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,‘王丽’ ,‘女’ ,to_date(‘1976-01-23’,‘yyyy-mm-dd’),95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,‘李军’ ,‘男’ ,to_date(‘1976-02-20’,‘yyyy-mm-dd’),95033);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,‘王芳’ ,‘女’ ,to_date(‘1975-02-10’,‘yyyy-mm-dd’),95031);

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,‘陆君’ ,‘男’ ,to_date(‘1974-06-03’,‘yyyy-mm-dd’),95031);

commit;*/

/*INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-105’ ,‘计算机导论’,825);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-245’ ,‘操作系统’ ,804);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘6-166’ ,‘数据电路’ ,856);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘9-888’ ,‘高等数学’ ,100);

commit;*/

/*INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-245’,86);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-245’,75);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-245’,68);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-105’,92);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-105’,88);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-105’,76);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘3-105’,64);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘3-105’,91);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘3-105’,78);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘6-166’,85);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘6-106’,79);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘6-166’,81);

commit;*/

/*INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,‘李诚’,‘男’,to_date(‘1958-12-02’,‘yyyy-mm-dd’),‘副教授’,‘计算机系’);

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,‘张旭’,‘男’,to_date(‘1969-03-12’,‘yyyy-mm-dd’),‘讲师’,‘电子工程系’);

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,‘王萍’,‘女’,to_date(‘1972-05-05’,‘yyyy-mm-dd’),‘助教’,‘计算机系’);

INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,‘刘冰’,‘女’,to_date(‘1977-08-14’,‘yyyy-mm-dd’),‘助教’,‘电子工程系’);

commit;*/

–1、查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class
from student1;

–2、查询教师所有的单位即不重复的Depart列。
select distinct depart
from teacher1;

–3、查询Student表的所有记录。
select * from student1;

–4、查询Score表中成绩在60到80之间的所有记录。
select * from score1
where degree<80 and degree>60;

–5、查询Score表中成绩为85,86或88的记录。
select * from score1
where degree=85 or degree=86 or degree=88;

–6、查询Student表中“95031”班或性别为“女”的同学记录。
select * from student1
where class=‘95031’ or ssex=‘女’;

–7、以Class降序查询Student表的所有记录。
select * from student1
order by class desc;

–8、以Cno升序、Degree降序查询Score表的所有记录。
select * from score1
order by cno asc,degree desc;

–9、查询“95031”班的学生人数。
select count(1)
from student1
where class=‘95031’;

–10、查询Score表中的最高分的学生学号和课程号。
select *
from (select sno,cno,degree
from score1
group by sno,cno,degree
order by degree desc)
where rownum<2;

–11、查询‘3-105’号课程的平均分。
select avg(degree)
from score1
where cno=‘3-105’;

–12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
in ----- exists
select cno,avg(degree)
from score1
where cno in (select cno
from score1
where cno like ‘3%’
group by cno
having count(cno)>=5)
group by cno;

–13、查询最低分大于70,最高分小于90的Sno列。
select sno
from score1
group by sno
having min(degree)>70 and max(degree)<90;

–14、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree
from student1,score1
where student1.sno=score1.sno
group by sname,cno,degree;

–17、查询“95033”班所选课程的平均分。
select avg(degree)
from score1,student1
where student1.sno=score1.sno and class=‘95033’;

–19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select sno,cno,degree from score1
where cno=‘3-105’ and degree>(select degree
from score1 where sno=‘109’ and cno=‘3-105’);

(有问题)–20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select * from (select sno,cno,degree from score1
group by sno,cno,degree
having count(cno)>1
order by sno asc,degree asc)
where degree != (select max(degree) from(select sno,cno,degree from score1
group by sno,cno,degree
having count(cno)>1));

–21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select distinct * from score1
where degree>(select degree
from score1
where sno=‘109’ and cno=‘3-105’);

–22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday
from student1
where to_char(sbirthday,‘yyyy’)=(select to_char(sbirthday,‘yyyy’) from student1 where sno=‘108’)
and sno<>‘108’;

–23、查询“张旭“教师任课的学生成绩。
select degree
from teacher1,score1,course1
where teacher1.tno=course1.tno and score1.cno=course1.cno and tname=‘张旭’;

–24、查询选修某课程的同学人数多于5人的教师姓名
select tname
from teacher1
where tno=(select tno from (select tno,course1.cno from score1,course1
where score1.cno=course1.cno group by tno,course1.cno
having count(1)>5));

–25、查询95033班和95031班全体学生的记录。
select * from student1
where class in (‘95033’,‘95031’)
order by class asc;

–26、查询存在有85分以上成绩的课程Cno.
select distinct cno
from score1
where degree>85;

–27、查询出“计算机系“教师所教课程的成绩表。
select course1.tno,degree
from score1,course1,teacher1
where score1.cno=course1.cno and teacher1.tno=course1.tno
and depart=‘计算机系’
group by course1.tno,degree;

–32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname,ssex,sbirthday,tname,tsex,tbirthday
from student1,course1,score1,teacher1
where student1.sno=score1.sno and
score1.cno=course1.cno and
teacher1.tno=course1.tno and
ssex=‘女’ and tsex=‘女’
group by sname,ssex,sbirthday,tname,tsex,tbirthday;—不全

分步查询
女学生:
select sname,ssex,sbirthday
from student1
where ssex=‘女’
group by sname,ssex,sbirthday;

女教师:
select tname,tsex,tbirthday
from teacher1
where tsex=‘女’
group by tname,tsex,tbirthday;

–33、查询成绩比该课程平均成绩低的同学的成绩表。
select sno,cno,degree
from score1 b
where degree<(select avg(degree) from score1 a where a.cno=b.cno)
group by sno,cno,degree;

–34、查询所有任课教师的Tname和Depart.
select distinct tname,depart
from teacher1,course1,score1
where score1.cno=course1.cno and teacher1.tno=course1.tno;

–35、查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher1
where tname not in (select distinct tname
from teacher1,course1,score1
where score1.cno=course1.cno and teacher1.tno=course1.tno);

–36、查询至少有2名男生的班号。
select class
from student1
where ssex=‘男’
group by class
having count(ssex)>=2;

–37、查询Student表中不姓“王”的同学记录。
select * from student1
where sname not like ‘王%’;

–38、查询Student表中每个学生的姓名和年龄。
select sname,floor(months_between(sysdate,sbirthday)/12) “年龄”
from student1;

–39、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday)
from student1;

–40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select sno,sname,ssex,sbirthday,class,floor(months_between(sysdate,sbirthday)/12) “年龄”
from student1
group by sno,sname,ssex,sbirthday,class
order by class desc,“年龄” desc;

–41、查询“男”教师及其所上的课程。
select course1.cno,tname,tsex,cname
from course1,teacher1,score1
where score1.cno=course1.cno and teacher1.tno=course1.tno and tsex=‘男’
group by course1.cno,tname,tsex,cname;

–42、查询最高分同学的Sno、Cno和Degree列。
select *
from (select sno,cno,degree from score1 group by sno,cno,degree
order by degree desc)
where rownum <2;

–43、查询和“李军”同性别的所有同学的Sname.
select sname
from student1
where ssex=(select ssex from student1 where sname=‘李军’) and sname !=‘李军’;

–44、查询和“李军”同性别并同班的同学Sname.
select sname
from student1
where ssex=(select ssex from student1 where sname=‘李军’) and
class=(select class from student1 where sname=‘李军’) and
sname !=‘李军’;

–45、查询所有选修“计算机导论”课程的“男”同学的
select student1.sno,sname,ssex,degree
from student1,score1,course1
where score1.cno=course1.cno and student1.sno=score1.sno and cname=‘计算机导论’ and
ssex=‘男’
group by student1.sno,sname,ssex,degree;

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值