Oracle常见习题及多种解法

-- 选了数学的名字
select sname from STUDENT,sc,COURSE
where sc.sno=STUDENT.SNO and sc.CNO=COURSE.CNO and COURSE.CNAME='数学';

-- 只选了数据库的名字(分解为只选了一门的且选的数据库)
select SNAME from STUDENT,COURSE,sc
where STUDENT.SNO in(select sc.SNO from sc
group by sc.sno
having count(*)=1 ) and sc.sno=STUDENT.SNO and sc.CNO=COURSE.CNO and COURSE.CNAME = '数据库';

-- 只选了1门课的(select 只能搜出以分组的东西,例如以学号分组,他组里面就学号了)
select distinct SNAME from STUDENT,COURSE,sc
where STUDENT.SNO in(select sc.SNO from sc
group by sc.sno
having count(*)=1);

-- 选了数据库还选了其他的名字
select SNAME from STUDENT,COURSE,sc
where STUDENT.SNO in(select sc.SNO from sc
group by sc.sno
having count(*)>1 ) and sc.sno=STUDENT.SNO and sc.CNO=COURSE.CNO and COURSE.CNAME = '数据库';


-- 选了数学和数据库的名字
-- 解法1 交集
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='信息系统'
intersect
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='数学';
-- 解法2 笛卡尔积
select SNAME
from STUDENT
where sno in (
    select x.sno from sc x,sc y where x.cno=(select cno from COURSE where CNAME='信息系统')
                                and y.CNO=(select cno from COURSE where CNAME='数学')
                                and x.sno=y.sno
    );


-- 只选了数学和数据库的名字
insert into sc values ('6','2',99);commit ;
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='信息系统'
intersect
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='数学'
and STUDENT.SNO in(
    select sno
    from sc
    group by sc.SNO
    having count(*)=2 );


-- 选了数学或数据库的名字
-- 解法1
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='信息系统'
union
select SNAME from sc,COURSE,STUDENT where sc.SNO= STUDENT.sno and COURSE.CNO=SC.CNO and COURSE.CNAME='数学';
-- 解法2
select distinct SNAME from sc,COURSE,STUDENT
where sc.SNO= STUDENT.sno
and COURSE.CNO=SC.CNO
and (COURSE.CNAME='信息系统' or COURSE.CNAME='数学');




-- 没有选数学的名字
-- 解法一
select distinct SNAME from STUDENT,sc,COURSE
where STUDENT.sno=sc.sNO and COURSE.Cno=sc.CNO
minus
select distinct SNAME from STUDENT,sc,COURSE
where STUDENT.sno=sc.sNO and COURSE.Cno=sc.CNO and CNAME in '数学';
-- 解法二 选课的不在选数学的里
select distinct SNAME from STUDENT,sc where STUDENT.sno not in(
select sno from sc,COURSE where COURSE.CNO=sc.CNO and CNAME = '数学'
    ) and sc.sno=STUDENT.SNO;


-- 没有选数学和数据库的名字
select distinct SNAME from STUDENT,sc,COURSE
where STUDENT.sno=sc.sNO and COURSE.Cno=sc.CNO
minus
select distinct SNAME from STUDENT,sc,COURSE
where STUDENT.sno=sc.sNO and COURSE.Cno=sc.CNO and CNAME in '数学'
minus
select distinct SNAME from STUDENT,sc,COURSE
where STUDENT.sno=sc.sNO and COURSE.Cno=sc.CNO and CNAME in '数据库';

insert into sc values ('20','1',77);
insert into sc values ('20','2',77);
insert into sc values ('20','3',77);
insert into sc values ('20','4',77);
insert into sc values ('20','5',77);
insert into sc values ('20','6',77);
insert into sc values ('20','7',77);
commit ;

-- 全选课的人名字
-- 解法一
select SNAME
from STUDENT
where sno in(
    select sno
    from sc group by sno having count(*)=(select count(*)from COURSE)
    );
-- 解法二 不存在不选所有课的情况
select SNAME
from STUDENT
    where sno in (
        select sno from sc x where not exists(select cno from COURSE where not exists(
            select sno  from sc y where y.sno= x.sno and y.cno=COURSE.cno)));



-- 至少选修了8号学生选课的学生
-- select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno x选了y也选了,返回true,不存在x选了y不选,双重否定两个not exists
select distinct sname
from STUDENT
where sno in (select sno from sc scx where not exists
    (select sno from sc scy where scy.sno='8'and not exists(select sno from sc scz where scz.sno=scx.sno and scz.cno=scy.cno ))
    );

猜你喜欢

转载自blog.csdn.net/AAliuxiaolei/article/details/116071212