北京华健面试题答案(oracle)

来源:互联网 发布:vmware10 for mac下载 编辑:程序博客网 时间:2024/06/06 05:13
--1、问题描述:
--已知关系模式:
--S (SNO,SNAME)学生关系。SNO 为学号,SNAME 为姓名
--C (CNO,CNAME,CTEACHER)  课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
--SC(SNO,CNO,SCGRADE)        选课关系。SCGRADE 为成绩


create table s(sno int, sname varchar(10));insert into s values(100,'张三');insert into s values(101,'李四');insert into s values(102,'王五');insert into s values(103,'赵六');insert into s values(104,'田七');insert into s values(105,'王八');

create table c(cno int, cname varchar(10),cteacher varchar(10));insert into c values(1,'c语言','李明');insert into c values(2,'sql','李果');

create table sc(sno int,cno int,scgrade int);insert into sc values(100,1,50);insert into sc values(100,2,55);insert into sc values(101,2,87);insert into sc values(102,1,50);insert into sc values(102,2,55);insert into sc values(103,1,80);insert into sc values(103,2,55);insert into sc values(104,1,90);insert into sc values(105,1,85);

select * from s;select * from c;select * from sc;




--1.找出没有选修过“李明”老师讲授课程的所有学生姓名
select sname from s where sno in(select sno  from sc where cno not in(select cno from c where cteacher ='李明'));

--2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
select (select sname from s where s.sno=sc.sno) as 姓名,avg(scgrade) from sc where scgrade<60 group by sno having count(*)>=2;

--3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
select sname from s where sno in(select sno from sc where cno in(1,2) group by sno having count(*)=2);

--4.列出“1”号课成绩比“2”号课程成绩高的所有学生的学号
select sc1.sno from sc sc1,sc sc2 where sc1.cno=1 and sc2.cno=2 and sc1.sno=sc2.sno and sc1.scgrade>sc2.scgrade;

--5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
select sc1.sno,sc1.scgrade "1号课成绩",sc2.scgrade "2号课成绩" from sc sc1,sc sc2 where sc1.cno=1 and sc2.cno=2 and sc1.sno=sc2.sno and sc1.scgrade>sc2.scgrade;


--2、问题描述:为管理岗位业务培训信息,建立3个表:
--S (Sno,SName,SDept,SAge)   S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
--C (Cno,CName )        C#,CN       分别代表课程编号、课程名称
--SC ( Sno,Cno,Grade )    S#,C#,G     分别代表学号、所选修的课程编号、学习成绩

-- 学生信息表
--字段分别代表:学号、学员姓名、所属单位、学员年龄
create table stu(Sno int,SName varchar(10), SDept varchar(50) ,SAge int );insert into stu values(1,'小明','税务局',26);insert into stu values(2,'小王','铁路局',27);insert into stu values(3,'小和','电力局',28);insert into stu values(4,'小宝','文化局',29);

--课程信息表
--字段分别代表:课程编号、课程名称
create table cc(Cno char(2), CName varchar(20));insert into cc values('c1','计算机原理');insert into cc values('c2','经济学原理');insert into cc values('c3','税收基础');insert into cc values('c4','C语言基础');insert into cc values('c5','中国刑法');insert into cc values('c6','心理学');insert into cc values('c7','会计学');


--选课信息表
--字段分别代表代表学号、所选修的课程编号、学习成绩
create table scc(Sno int, Cno char(2) , Grade int);insert into scc values(1,'c1',60);insert into scc values(1,'c2',70);insert into scc values(1,'c3',80);insert into scc values(1,'c4',90);insert into scc values(1,'c5',100);insert into scc values(1,'c6',65);insert into scc values(2,'c2',65);insert into scc values(3,'c1',60);insert into scc values(3,'c2',70);insert into scc values(3,'c3',80);insert into scc values(3,'c4',90);insert into scc values(3,'c5',100);insert into scc values(3,'c6',65);insert into scc values(3,'c7',70);

select * from stu;select * from cc;select * from scc;


--要求实现如下5个处理:
--1. 使用标准SQL嵌套语句查询选修了课程名称为’税收基础’的学员学号和姓名
select sno,sname from stu where sno in(select sno from scc where cno=(select cno from cc where cname='税收基础'));select sno,(select sname from stu where stu.sno=scc.sno) from scc where cno=(select cno from cc where cname='税收基础');

--2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
select sname,sdept from stu where sno in(select sno from scc where cno='c2');

--3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
select sname,sdept from stu where sno in((select sno from stu) minus (select sno from scc where cno='c5'));

--4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位
select sname,sdept from stu where sno in(select sno from scc group by sno having count(*)=(select count(*) from cc));

--5. 查询选修了课程的学员人数
select count(distinct sno) from scc;

--6. 查询选修课程超过5门的学员学号和所属单位
select sno,sdept from stu where sno in(select sno from scc group by sno having count(distinct cno)>5);

0 0
原创粉丝点击