数据库精通练习题答案

CREATE TABLE students
(sno VARCHAR2(30) NOT NULL,
sname VARCHAR2(30) NOT NULL,
ssex VARCHAR2(30) NOT NULL,
sbirthday DATE,
class VARCHAR2(30));
select * from students
CREATE TABLE courses
(cno VARCHAR2(30) NOT NULL,
cname VARCHAR2(30) NOT NULL,
tno VARCHAR2(30) NOT NULL);
select * from courses
CREATE TABLE scores
(sno VARCHAR2(30) NOT NULL,
cno VARCHAR2(30) NOT NULL,
degree number(10) NOT NULL);

CREATE TABLE teachers
(tno VARCHAR2(30) NOT NULL,
tname VARCHAR2(30) NOT NULL, tsex VARCHAR2(30) NOT NULL,
tbirthday DATE NOT NULL, prof VARCHAR2(30),
depart VARCHAR2(30) NOT NULL);
select * from teachers
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,‘曾华’ ,‘男’ ,to_date(‘1977-09-01’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,‘匡明’ ,‘男’ ,to_date(‘1975-10-02’, ‘yyyy-mm-dd’),95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,‘王丽’ ,‘女’ ,to_date(‘1976-01-23’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,‘李军’ ,‘男’ ,to_date(‘1976-02-20’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,‘王芳’ ,‘女’ ,to_date(‘1975-02-10’, ‘yyyy-mm-dd’),95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,‘陆君’ ,‘男’ ,to_date(‘1974-06-03’, ‘yyyy-mm-dd’),95031);

INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘3-105’ ,‘计算机导论’,825);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘3-245’ ,‘操作系统’ ,804);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘6-166’ ,‘数据电路’ ,856);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘9-888’ ,‘高等数学’ ,100);

INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,‘3-245’,86);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,‘3-245’,75);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,‘3-245’,68);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,‘3-105’,92);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,‘3-105’,88);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,‘3-105’,76);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,‘3-105’,64);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,‘3-105’,91);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,‘3-105’,78);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,‘6-166’,85);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,‘6-106’,79);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,‘6-166’,81);

INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,‘李诚’,‘男’,to_date(‘1958-12-02’, ‘yyyy-mm-dd’),‘副教授’,‘计算机系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,‘张旭’,‘男’,to_date(‘1969-03-12’, ‘yyyy-mm-dd’),‘讲师’,‘电子工程系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,‘王萍’,‘女’,to_date(‘1972-05-05’, ‘yyyy-mm-dd’),‘助教’,‘计算机系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,‘刘冰’,‘女’,to_date(‘1977-08-14’, ‘yyyy-mm-dd’),‘助教’,‘电子工程系’);

select * from te
1、 查询Students表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from students
2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teachers
3、 查询Students表的所有记录。
select * from students
4、 查询Scores表中成绩在60到80之间的所有记录。
select * from scores
select degree from scores where degree between 60 and 80;
5、 查询Scores表中成绩为85,86或88的记录。
select degree from scores where degree = 85 or degree = 86 or degree = 88;
6、 查询Students表中“95031”班或性别为“女”的同学记录。
select * from students
select class,ssex from students where class = ‘95031’ or ssex =‘女’;
7、 以Class降序查询Students表的所有记录。
select class from students order by class desc;
8、 以Cno升序、Degree降序查询Scores表的所有记录。
select * from scores
select cno,degree from scores order by cno desc,cno asc;
9、 查询“95031”班的学生人数。
select * from students
select count(*) from students where class =‘95031’;
10、查询Scores表中的最高分的学生学号和课程号。

–bu
select sname from students 学号
(select max(degree) from scores)
join scores 学浩 on 学号 sno = 学浩 sno
;
11、查询‘3-105’号课程的平均分。
select avg(degree) from scores where CNO = ‘3-105’;
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

13、查询最低分大于70,最高分小于90的Sno列。
select sno from scores where degree between 70 and 90;
14、查询所有学生的Sname、Cno和Degree列。
select sname,b.cno,degree from students a join scores b on a.sno = b.sno;

15、查询所有学生的Sno、Cname和Degree列。
select * from courses
select * from students
select * from scores

select Sno,cname,Degree from scores a join courses b on a.cno = b.cno

16、查询所有学生的Sname、Cname和Degree列。

select sname,cname,degree from scores a, courses b, students c where a.cno = b.cno and c.sno = a.sno;

select sname,cname,degree from students t join scores s on t.sno = s.sno join courses c on s.cno = c.cno;

17、查询“95033”班所选课程的平均分。

select avg(degree) from scores where degree in
(select degree from students a, scores c where a.sno =c.sno and class = 95033);

18、假设使用如下命令建立了一个grade表:
create table grade(low number(10),upp number(10),rank varchar2(10));
insert into grade values(90,100,‘A’);
insert into grade values(80,89,‘B’);
insert into grade values(70,79,‘C’);
insert into grade values(60,69,‘D’);
insert into grade values(0,59,‘E’);
select * from grade
现查询所有同学的Sno、Cno和rank列。
select * from grade
select * from scores
select * from students
select sno,cno,rank from scores a join grade b on degree between low and upp;
select sno,cno,rank from scores a,grade b where degree between low and upp;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from scores where degree >all (select degree from scores where sno=109) and cno = ‘3-105’

select * from scores where degree >
(select degree from scores where cno ='3-105’and sno = 109) and cno = ‘3-105’

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
(select * from scores a join students b on a.sno = b.sno)
select * from students

select * from scores where degree not in(
select max(degree) from scores group by sno having count(sno) >1);

select degree from scores where degree not in
(select max(degree) from scores group by sno having count (sno) >1)
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from scores where degree >all
(select degree from scores where cno = ‘3-105’ and sno = 109) and cno = ‘3-105’;

22、查询“张旭“教师任课的学生成绩。
select * from teachers
select * from students
select * from scores
select * from courses
select * from scores a where a.cno in(
select cno from courses where tno in(
select tno from teachers where tname = ‘张旭’));

23、查询选修某课程的同学人数多于5人的教师姓名。

select tname from teachers t join courses c on t.tno = c.tno where cno =
(select cno from scores group by cno having count(cno)>5)

24、查询95033班和95031班全体学生的记录。
select * from students
select class
select * from students where class =any
(select * from students where class in (95033 ,95031) );
25、查询存在有85分以上成绩的课程Cno.
select distinct cno from scores where degree > 85
26、查询出“计算机系“教师所教课程的成绩表。
select * from scores where cno in(
select cno from courses where tno in
(select tno from teachers where depart = ‘计算机系’));
select * from scores where cno in
(select cno from teachers a,courses b where a.tno = b.tno and depart = ‘计算机系’);
27、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

select * from teachers where prof in
(select prof from teachers group by prof having count(prof)=1)and depart in( ‘计算机系’ ,‘电子工程系’);

28、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select * from courses where cno = ‘3-105’;
select * from scores where degree > any
(select degree from scores where cno = ‘3-105’)and cno =‘3-245’ order by degree desc;

29、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select * from courses where cno = ‘3-105’
select * from scores where degree >all
(select degree from scores where cno = ‘3-245’)and cno =‘3-105’;

30、查询所有教师和同学的name、sex和birthday.
select tname,tsex,tbirthday from teachers
union
select sname,ssex,sbirthday from students

31、查询所有“女”教师和“女”同学的name、sex和birthday.
select tname,tsex,tbirthday from teachers where tsex = ‘女’
union
select sname,ssex,sbirthday from students where ssex = ‘女’;
32、查询成绩比该课程平均成绩低的同学的成绩表。
select * from scores where
(select avg(degree) from scores );

select * from scores c where degree < (
select avg(degree) from scores e group by cno having e.cno = c.cno);

select * from scores;
33、查询所有任课教师的Tname和Depart.
34 查询所有未讲课的教师的Tname和Depart.
35、查询至少有2名男生的班号。
select distinct class frjava sources om students where class in
(select class from students group by class having count (ssex)>1 ) and ssex=‘男’ ;
36、查询Student表中不姓“王”的同学记录。
select sname from students where sname not like ‘王%’;
37、查询Student表中最大和最小的Sbirthday日期值。
38、以班号和年龄从大到小的顺序查询Student表中的全部记录。
39、查询“男”教师及其所上的课程。
40、查询最高分同学的Sno、Cno和Degree列。
select sno,cno,degree from scores where degree in
(select max(degree) from scores);
41、查询和“李军”同性别的所有同学的Sname.
42、查询和“李军”同性别并同班的同学Sname.
select sname from students where ssex in
(select ssex from students where sname = ‘李军’)
and class in
(select class from students where sname = ‘李军’)
and sname != ‘李军’;

select * from students where class in(
select class from students where ssex =
(select ssex from students where sname = ‘李军’ ))and sname = ‘李军’;
43、查询所有选修“计算机导论”课程的“男”同学的成绩表

select ssex, o.cno,o.sno,degree from
students s ,scores o,courses c where s.sno = o.sno
and o.cno = c.cno and cname = ‘计算机导论’ and ssex = ‘男’;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘿爱多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值