Oracle语法 50道sql语句练习

本文提供了50个使用Oracle SQL进行复杂查询的示例,涵盖了成绩对比、平均分计算、特定课程分析等多种场景。通过这些示例,读者可以深入理解如何在实际操作中运用SQL解决数据库查询问题。
摘要由CSDN通过智能技术生成

/*

Student(S#,Sname,Sage,Ssex)学生表

Course(C#,Cname,T#)课程表

SC(S#,C#,score)成绩表

Teacher(T#,Tname)教师表

*/

–1、查询“001”课程比“002”课程成绩高的所有学生的学号;

select s1.s#

from SC s1,SC s2

where s1.s#=s2.s# and s1.c#='001’and s2.c#='002’ands1.score>s2.score;

–2、查询平均成绩大于60分的同学的学号和平均成绩;

select s.s#,avg(s.score)

from SC s

groupby s.s#

havingavg(s.score)>60;

–3、查询所有同学的学号、姓名 、选课数、总成绩;

select d.s#,max(d.sname),count(distinct s.c#),sum(s.score)

from Student d,SC s

where d.s#=s.s#

groupby d.s#;

–4、查询姓“李”的老师的个数;

selectcount(0)

from Teacher t

where t.tname like’李%’;

–5、查询没学过“叶平”老师课的同学的学号、姓名;

select d.s#,d.sname

from Student d

wherenotexists(

selectdistinct(s.s#)

from SC s,Teachert,Course c

where t.t#=c.t# and s.c#=c.c# and t.tname='叶平’and d.s#=s.s#);

–6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select d.s#, d.sname

from Student d, SC s

where s.s# = d.s# and s.c# ='001’andexists

(select s1.s# from SC s1 where s1.s# = s.s# and s1.c# =‘002’);

–7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select s.s#,max(d.sname)

from SC s,Studentd,Course c,Teachert

where s.s#=d.s# and s.c#=c.c# and c.t#=t.t# and t.tname=‘叶平’

groupby s.s#

havingcount(distinct s.c#)=(

selectcount(0)from Coursec,Teachert where c.t#=t.t# and t.tname=‘叶平’);

–8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

select d.s#,d.sname

from SC s1,SC s2,Studentd

where s1.s#=s2.s# and s1.s#=d.s# and s1.c#='002’and s2.c#='001’ands1.score<s2.score;

–9、查询所有课程成绩小于60分的同学的学号、姓名;

select s.s#,max(d.sname)

from Student d,SC s

where d.s#=s.s# and s.score<60

groupby s.s#

havingcount(0)=(selectcount(0)from SC s1 where s.s#=s1.s#);

–10、查询没有学全所有课的同学的学号、姓名;

select s.s#,max(d.sname)

from Student d,SC s

where d.s#=s.s#

groupby s.s#

havingcount(distinct s.c#)<>(selectcount(0)from Course);

–11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

selectdistinct s.s#,d.sname

from Student d,SC s

where d.s#=s.s# and s.s#<>'1001’andexists(

select s1.c# from SC s1 where s1.s#='10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值