您的位置:首页 > 数据库 > Oracle

50道经典SQL语句题目及答案(使用Oracle语法)

2014-04-26 21:56 537 查看
/*
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#='1001'and
s.c#=s1.c#
);

--12、查询至少学过学号为“1010”同学所有一门课的其他同学学号和姓名;

selectdistinct s.s#,d.sname
from Student d,SC s
where d.s#=s.s#
and s.s#<>'1010'and
s.c# in(
select s1.c#
from SC s1
where s1.s#='1010');

--13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

update SC s
set s.score
=
(selectavg(s1.score)from
SC s1 where s.c#
= s1.c#)
whereexists(select
s.c#
from Coursec,
Teachert
where s.c#
= c.c#
and c.t#
= t.t#
and t.tname='叶平');

--14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

select s.s#,max(d.sname)
from Studentd,SC s
where d.s#=s.s#
and s.s#<>'1002'andexists(
select c#
from SC
where s#='1002'and
s.c#=c#)
groupby s.s#
havingcount(0)=(selectcount(0)from
SC where s#='1002')
and count((select count(0) from SC wheres#=s.s#))=(select count(0) from SC where s#='1002');

--15、删除学习“叶平”老师课的SC表记录;

delete SC s
whereexists(select
s.c# from Coursec,Teachert
where s.c#=c.c#
and c.t#=t.t#
and t.tname='叶平');

--16、向SC表中插入一些记录,这些记录要求符合以下条件:
--
没有上过编号“003”课程的同学学号、002号课的平均成绩;

insertinto SC

(select d.s#,'002',(selectround(avg(score),2)from
SC where c#='002')
from Student d
wherenotexists(selectdistinct
s.s# from SC s
where s.s#=d.s#
and s.c#='003'));

--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,
--
按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分;

select s.s#
as学生ID,
(select avg(s1.score)from
SC s1,Course c
where s1.c#=c.c#
and s.s#=s1.s#
andc.cname='Java编程思想')as
Java编程思想,
(select avg(s1.score)from
SC s1,Course c
where s1.c#=c.c#
and s.s#=s1.s#
andc.cname='Struts2基础')as
Struts2基础,
(select avg(s1.score)from
SC s1,Course c
where s1.c#=c.c#
and s.s#=s1.s#
andc.cname='Spring基础')as
Spring基础,
count(0)as有效课程数,round(avg(s.score),2)as有效平均分
from SC s,Course c
where s.c#=c.c#
andc.cname
in('Java编程思想','Struts2基础','Spring基础')
groupby s.s#
orderbyround(avg(s.score),2)desc;

--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select s.c#
as课程ID,max(s.score)as最高分,min(s.score)as最低分
from SC s
groupby s.c#;

--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

select s.c#
as课程ID,round(avg(s.score),2)as平均分,
round(100*sum(casewhen
s.score>=60then1else0end)/count(s.score),0)||'%'as及格率

from SC s
groupby s.c#;

--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):

--
企业管理(001),马克思(002),OO&UML(003),数据库(004)

selectround(sum(casewhen
c.cname='Java编程思想'then
s.score else0end)/sum(case
c.cnamewhen'Java编程思想'then1else0end),2)as
Java编程思想,
round(100*sum(casewhen
s.score>=60and c.cname='Java编程思想'then1else0end)/sum(casewhen
c.cname='Java编程思想'then1else0end),0)||'%'as及格率,
round(sum(casewhen
c.cname='Struts2基础'then
s.scoreelse0end)/sum(case
c.cnamewhen'Struts2基础'then1else0end),2)as
Struts2基础,

round(100*sum(casewhen
s.score>=60and c.cname='Struts2基础'then1else0end)/sum(casewhen
c.cname='Struts2基础'then1else0end),0)||'%'as及格率
from SC s,Course c
where s.c#=c.c#;

--21、查询不同老师所教不同课程平均分从高到低显示

--
SELECT max(Z.T#) AS 教师ID,MAX(Z.Tname) AS
教师姓名,

--
C.C# AS 课程ID,MAX(C.Cname) AS 课程名称,AVG(Score) AS 平均成绩

select t.t#
as教师ID,
max(t.tname)as教师名称,
c.cname
as所教课程,
round(avg(s.score),2)as平均分
from Teacher t, Coursec,
SC s
where t.t#
= c.t#
and c.c#
= s.c#
groupby t.t#,
c.cname
orderby t.t#,round(avg(s.score),2)desc;

--22、查询如下课程成绩第 3
名到第6 名的学生成绩单:

--
企业管理(001),马克思(002),UML (003),数据库(004)

select d.s#,d.sname,c.cname,s.score,s.rank1
as名次
from Student d,Course c,
(select s#,c#,score,row_number()over(partitionby
c# orderby score
desc) rank1
from SC) s
where s.c#=c.c#
and d.s#=s.s#

and c.cnamein('Java编程思想','Struts2基础','Hibernate基础','Spring基础')
and(s.rank1
between3and6)
orderby c.c#,s.rank1;

--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select s.c#,max(c.cname),
sum(casewhen
s.scorebetween85and100then1else0end)as分数85至100,
sum(casewhen
s.scorebetween70and85then1else0end)as分数70至85,
sum(casewhen
s.scorebetween60and70then1else0end)as分数60至70,
sum(casewhen
s.scorebetween0and60then1else0end)as小于60
from SC s,Course c
where s.c#=c.c#
groupby s.c#;

--24、查询学生平均成绩及其名次

select d.s#,max(d.sname),round(avg(s.score),2)as平均分,
dense_rank()over(orderbyavg(s.score)desc)as名次
from Student d,SC s
where d.s#=s.s#
groupby d.s#;

--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

select c.c#
as课程编号,c.cname
as课程名称,
d.sname
as学生名称,s.rank1
as名次,s.score
as成绩
from(select s#,c#,score,
row_number()over(partitionby
c# orderby score
desc) rank1
from sc
) s,Course c,Studentd
where s.c#=c.c#
and s.s#=d.s#
and
rank1 <4
orderby c.c#,s.rank1;

--26、查询每门课程被选修的学生数

select s.c#,count(distinct
s.s#)as人数
from SC s
groupby s.c#;

--27、查询出只选修了一门课程的全部学生的学号和姓名

select d.s#,max(d.sname)
from SC s,Studentd
where d.s#=s.s#
groupby d.s#
havingcount(distinct
s.c#)=1;

--28、查询男生、女生人数

selectsum(case
d.ssex when'男'then1else0end)as男生,
sum(case
d.ssex when'女'then1else0end)as女生
from Student d;

--29、查询姓“张”的学生名单

select*
from Student d
where d.sname
like'张%';

--30、查询同名同性学生名单,并统计同名人数

select d.sname,d.ssex,count(0)
from Student d
groupby d.sname,d.ssex
havingcount(0)>1;

--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select*
from Student d
where to_char(d.sage,'yyyy')='1981';

--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select s.c#,round(avg(s.score),2)as平均分
from SC s
groupby s.c#
orderbyavg(s.score),s.c#
desc;

--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select s.s#,max(d.sname),round(avg(s.score),2)as平均分
from SC s,Studentd
where s.s#=d.s#
groupby s.s#
havingavg(s.score)>85;

--34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
select d.sname,s.score
from Student d,SC s,Course
c
where d.s#=s.s#
and s.c#=c.c#
and c.cname='Java编程思想'and
s.score<60;

--35、查询所有学生的选课情况;

select d.s#,max(d.sname),count(distinct
s.c#)as选课数
from Student d
leftjoin SC s
on(d.s#=s.s#)
groupby d.s#;

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

selectmax(d.sname),max(c.cname),s.score
from Student d,SC s,Course
c
where d.s#=s.s#
and s.c#=c.c#
and s.score>70
groupby s.s#,s.score
havingcount(0)=(selectcount(s1.c#)from
SC s1 where s.s#=s1.s#);

--37、查询不及格的课程,并按课程号从大到小排列

select s.s#,c.cname,s.score
from SC s,Course c
where s.c#=c.c#
and s.score<60
orderby s.c#
desc;

--38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

select d.s#,d.sname
from Student d,SC s
where d.s#=s.s#
and s.c#='003'and
s.score>80;

--39、求选了课程的学生人数

selectcount(distinct
s.s#)as选课的人数
from SC s;

--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select sname,score

from(select d.sname,s.score,row_number()over(orderby
s.scoredesc)as rank1
from Studentd,SC
s,Course c,Teachert
where d.s#=s.s#
and s.c#=c.c#
and c.t#=t.t#
and t.tname='叶平')
where rank1=1;

--41、查询各个课程及相应的选修人数

select s.c#,count(distinct
s.s#)as选修人数

from SC s
groupby s.c#;

--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

select*
from SC s1,SC s2
where s1.c#<>s2.c#
ands1.score=s2.score
and s1.s#<>s2.s#;

--43、查询每门功成绩最好的前两名

select d.s#,d.sname,s.c#,s.score,s.rank1

from(select s1.s#,s1.c#,s1.score,
row_number()over(partitionby
s1.c# orderbys1.score
desc)as rank1

from SC s1)
s,Studentd
where s.s#=d.s#
and rank1
<3
orderby s.c#,s.rank1;

--44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,
--
查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列

select s.c#,count(distinct
s.s#)as选修人数
from Course c
leftjoin SC s
on(c.c#=s.c#)
groupby s.c#
havingcount(distinct
s.s#)>10
orderbycount(distinct
s.s#)desc,s.c#;

--45、检索至少选修两门课程的学生学号

select s.s#
from SC s
groupby s.s#
havingcount(distinct
s.c#)>=2;

--46、查询全部学生都选修的课程的课程号和课程名

select s.c#
as课程编号,
max(c.cname)as课程名称
from SC s,Course c
where s.c#=c.c#
groupby s.c#
havingcount(distinct
s.s#)=(selectcount(0)from
Student);

--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

select d.s#,d.sname
from Student d
wherenotexists(select
s.s# from SC s,Course c,Teachert
where s.c#=c.c#
and c.t#=t.t#
and s.s#=d.s#
and t.tname='叶平');

--48、查询两门以上不及格课程的同学的学号及其平均成绩

select s.s#,round(avg(s.score),2)as平均分
from SC s
where s.score<60
groupby s.s#
havingcount(s.c#)>2;

--49、检索“004”课程分数小于60,按分数降序排列的同学学号

select s.s#
from SC s
where s.c#='004'and
s.score<60
orderby s.scoredesc;

--50、删除“1002”同学的“001”课程的成绩

deletefrom SC s
where s.s#='1002'and
s.c#='001';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: