2019.1.8
首先建立三个表 STUDENT,SC,COURSE
CREATE TABLE "SC" (
"SNO" VARCHAR2(20),
"CNO" VARCHAR2(20),
"GRADE" VARCHAR2(20)
);
CREATE TABLE "STUDENT" (
"SNO" VARCHAR2(20),
"SNAME" VARCHAR2(20),
"SEX" VARCHAR2(20),
"DNO" VARCHAR2(20)
);
CREATE TABLE "COURSE" (
"CNO" VARCHAR2(20),
"CNAME" VARCHAR2(20),
"TNAME" VARCHAR2(20)
);
接着分别向三个表中插入数据
Insert into SC (SNO,CNO,GRADE) values ('0001','327401','70');
Insert into SC (SNO,CNO,GRADE) values ('1001','327401','80');
Insert into SC (SNO,CNO,GRADE) values ('1002','327401','90');
Insert into SC (SNO,CNO,GRADE) values ('1003','327401','85');
Insert into SC (SNO,CNO,GRADE) values ('1004','327401','87');
Insert into SC (SNO,CNO,GRADE) values ('1005','327401','86');
Insert into SC (SNO,CNO,GRADE) values ('1006','327401','56');
Insert into SC (SNO,CNO,GRADE) values ('1001','153701','85');
Insert into SC (SNO,CNO,GRADE) values ('1002','153701','60');
Insert into SC (SNO,CNO,GRADE) values ('1003','153701','70');
Insert into SC (SNO,CNO,GRADE) values ('1004','153701','65');
Insert into SC (SNO,CNO,GRADE) values ('1005','153701','90');
Insert into SC (SNO,CNO,GRADE) values ('1001','323232','90');
Insert into SC (SNO,CNO,GRADE) values ('1002','323232','40');
Insert into SC (SNO,CNO,GRADE) values ('1003','323232','60');
Insert into SC (SNO,CNO,GRADE) values ('1004','323232','70');
Insert into SC (SNO,CNO,GRADE) values ('1005','323232','80');
Insert into SC (SNO,CNO,GRADE) values ('1006','323232','70');
Insert into SC (SNO,CNO,GRADE) values ('1007','323232','50');
Insert into SC (SNO,CNO,GRADE) values ('1001','548963','90');
Insert into SC (SNO,CNO,GRADE) values ('1002','548963','40');
Insert into SC (SNO,CNO,GRADE) values ('1003','548963','60');
Insert into SC (SNO,CNO,GRADE) values ('1004','548963','70');
Insert into SC (SNO,CNO,GRADE) values ('1005','548963','80');
Insert into SC (SNO,CNO,GRADE) values ('1006','548963','70');
Insert into SC (SNO,CNO,GRADE) values ('1007','548963','50');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('0001','张三','男','10');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('0002','王蓉','女','20');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1001','里斯','男','10');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1003','赵丽','女','50');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1002','张三丰','男','10');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1004','张天','男','20');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1005','张海','男','10');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1006','张天涯','男','10');
Insert into STUDENT (SNO,SNAME,SEX,DNO) values ('1007','王丽丽','女','20');
Insert into COURSE (CNO,CNAME,TNAME) values ('327401','线性代数','赵蓉');
Insert into COURSE (CNO,CNAME,TNAME) values ('153701','高等数学','赵蓉');
Insert into COURSE (CNO,CNAME,TNAME) values ('323232','Java SE','TOM');
Insert into COURSE (CNO,CNAME,TNAME) values ('254689','Java Web','TIM');
Insert into COURSE (CNO,CNAME,TNAME) values ('548963','大学英语','ROSE');
相关题目和答案
--1求学生编号为’0001’的学生的学号、姓名、性别
select sno,sname,sex from student where sno=0001;
--2求学院编号(DNO)为’10’ 的男生的学号、姓名、性别
select sno,sname,sex from student where dno=10 and sex='男';
--3求选修授课号为‘327401’且成绩在80~90之间的学生学号和成绩,并将成绩乘以系数0.8输出,且将SNO列更名为学号,成绩列更名为处理成绩 。
select sno 学号,grade*0.8 处理成绩 from SC where cno=327401 and grade between 80 and 90;
select sno 学号,grade*0.8 处理成绩 from SC where cno=327401 and grade >= 80 and grade<=90;
select sno 学号,grade*0.8 处理成绩 from SC where cno=327401 and grade >80 and grade<90;
--4求选修了课程的学生的学号
select distinct(sno) from sc;
--5求姓名中包含’丽’的学生信息
select sno,sname,sex,dno from student where sname like '%丽%';
--6姓名是二个字的张姓同学
select sno,sname,sex,dno from student where sname like '张_';
--7所有姓张的同学
select sno,sname,sex,dno from student where sname like '张%';
--8统计选课学生人数及最高分成绩和最低分成绩
select count( distinct sno),max(grade),min(grade) from sc;
--9查询授课编号为‘153701’的课程的平均分
select avg(grade) from sc where CNO=153701;
--10统计各个学院的人数
select dno,count(sno) from student group by dno;
--11按课程号统计选修该课程的人数,并按照人数升序排列。
select cno, count(sno) from sc group by cno order by count(sno);
--12统计成绩超过80分的学生人数及平均成绩
select count(*),avg(grade) from sc where grade>80;
--13求学号为'0001'的同学的每门课的成绩,输出格式为:学号,课程名,课程成绩
--显式内连接
select sc.sno 学号,c.cname 课程名 ,sc.grade 课程成绩
from sc
inner join course c on sc.cno=c.cno
where sc.sno=0001;
--隐式内连接
select sno 学号,cname 课程名 ,grade 课程成绩
from sc,course c where sc.cno=c.cno and sno=0001;
--14查询每个学生的每门课程的成绩,要求输出学号,课程名,成绩
select sc.sno 学号,c.cname 课程名,sc.grade 成绩
from sc
inner join course c on sc.cno=c.cno;
--15查询每个学生的每门课程的成绩,要求输出学号,姓名,课程名,成绩
--显式内连接
select sc.sno 学号,s.sname 姓名,c.cname 课程名 ,sc.grade 成绩
from sc
inner join course c on sc.cno=c.cno
inner join student s on sc.sno=s.SNO;
--隐式内连接
select sc.sno 学号,s.sname 姓名,c.cname 课程名 ,sc.grade 成绩
from sc,course c,student s
where sc.cno=c.cno and sc.sno=s.sno;
--16使用嵌套查询方法,查询“赵蓉”教师任课的学生成绩,并按成绩递增排列
--在emp表中查询工资比ALLEN高的员工信息
select grade from sc where cno=any(select cno from course where TNAME ='赵蓉')order by grade;
--17查询成绩比该课程平均成绩低的学生成绩表(相关子查询)
select avg(grade) from sc group by CNO;--查询各课程平均成绩
-- select sno,cno, grade from sc where 课程X成绩<课程X平均成绩
select sno,cno, grade from sc s1 where grade <(select avg(grade) from sc s2 where s1.cno=s2.cno);
--18用子查询语句查询选修了“线性代数“的学生学号和姓名
select cno from course where CNAME ='线性代数';--在course表中根据课程名查询到课程号
select sno from sc where CNO=any( select cno from course where CNAME ='线性代数');
--在sc表中通过查询到的课程号查询到学生学号
select sno,sname from student
where SNO=any( select sno from sc where CNO=any( select cno from course where CNAME ='线性代数'));
--在student表中通过学号查询到学生姓名