Oracle 经典sql50题

Oracle分别先找到01和02的成绩,再作为表来源,最后进行比较1.1 查询同时存在" 01 “课程和” 02 "课程的情况跟上题一样,先分别找出两个课程,再写条件就行1.2 查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )nvl(a,b)a为空时,返回b左外连接就在右边的条件上加(+),右外连接就在左边加1.3 查询不存在" 01 “课程但存在” 02 "课程的情况先找到哪些学生选了01课程的id,去掉2. 查询平均成绩大于等于 60 分的同学
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前提

Oracle


提示:以下是本篇文章正文内容,下面案例可供参考

1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数

分别先找到01和02的成绩,再作为表来源,最后进行比较

select sname,sage,ssex,s1.score 课程01成绩,s2.score 课程02成绩
from student a,
(select score,sid from sc where cid='01') s1,
(select score,sid from sc where cid='02') s2
where a.sid=s1.sid and a.sid=s2.sid and s1.score>s2.score;

1.1 查询同时存在" 01 “课程和” 02 "课程的情况

跟上题一样,先分别找出两个课程,再写条件就行

select * 
from 
(select * from sc where cid='01') s1,
(select * from sc where cid='02') s2
where s1.sid=s2.sid;

1.2 查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )

nvl(a,b)a为空时,返回b
左外连接就在右边的条件上加(+),右外连接就在左边加

select s1.sid 学生id,s1.cid 课程01,s1.score 课程01的成绩,nvl(s2.cid,'null') 课程02,s2.score 课程02的成绩
from 
(select * from sc where cid='01') s1,
(select * from sc where cid='02') s2
where s1.sid=s2.sid(+);

1.3 查询不存在" 01 “课程但存在” 02 "课程的情况

先找到哪些学生选了01课程的id,去掉

select *
from sc
where sid not in (select sid from sc where cid='01') and cid='02';

2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

找到平均成绩大于等于60的作为表来源,查询即可

select s.sid,sname,c.score 平均成绩
from student s,
(select sid,avg(score) score
from sc
group by sid
having avg(score)>=60) c
where s.sid=c.sid
order by s.sid;

3. 查询在 SC 表存在成绩的学生信息

select distinct sc.sid,sname,sage,ssex
from student s,sc
where s.sid=sc.sid;

4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select s.sid,s.sname,scc.选课总数,scc.总成绩
from student s,(select sid,count(cid) 选课总数,sum(score) 总成绩 
from sc 
group by sid) scc
where s.sid=scc.sid(+);

4.1 查有成绩的学生信息

只要找到成绩表中有哪些学生id,对比就行

select sid,sname,sage,ssex
from student 
where exists(select sid from sc where student.sid=sc.sid);

5. 查询「李」姓老师的数量

select count(tname) 李姓老师数量
from teacher
where tname like '李%';

6. 查询学过「张三」老师授课的同学的信息

select s.sid,s.sname,s.sage,s.ssex
from student s,(select sc.sid
from teacher t,course c,sc
where t.tid=c.tid and sc.cid=c.cid and tname = '张三') scc
where s.sid=scc.sid;

7. 查询没有学全所有课程的同学的信息

先找到所有课程数,再对比

select s.sid,sname,sage,ssex
from student s,(select sid
from sc
group by sid
having count(cid)<(select count(cid)
from course)) scc
where s.sid=scc.sid ;

8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

先找到01的选课,再对比

select distinct sc.sid,sname,sage,ssex
from student s,sc
where s.sid=sc.sid and sc.cid in (select cid
from sc
where sid='01');

9. 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

按顺序拼接查询的课程,然后对比。
拼接字符串
listagg(合并字段,连接符) within group(order by 合并字段按什么排序) [over(partition by 分组字段)]
加上over,就是分析函数

先拼接01的课程
select listagg(cid,',') within group(order by cid)
from sc
where sid='01';

再拼接其他同学的课程
select listagg(cid,',') within group(order by cid)
from sc
where sid != '01'
group by sid;

对比,找到一样课程的同学id
select sid,sc1.ccid
from 
(select sid,listagg(cid,',') within group(order by cid) ccid
from sc
where sid != '01'
group by sid) sc1,
(select listagg(cid,',') within group(order by cid) ccid
from sc
where sid='01') sc2
where sc1.ccid = sc2.ccid;

最后根据id找到信息
select *
from student
where sid in (select sid
from 
(select sid,listagg(cid,',') within group(order by cid) ccid
from sc
where sid != '01'
group by sid) sc1,
(select listagg(cid,',') within group
  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值