声明:所有SQL语句均在实验平台验证通过,实验细节可能随时间推移老师会进行修改。在此仅提供解答思路,毕竟我的方法肯定不是最优,而且实验平台有查重功能,不要一昧的复制哦!
1.找出没有选修任何课程的学生的学号、姓名(即没有选课记录的学生)。
create view test2_01 as select sid,name from pub.student
where sid not in (select sid from pub.student_course);
2.找出至少选修了学号为"200900130417"的学生所选修的一门课的学生的学号、姓名。
create view test2_02 as
select pub.student.sid,pub.student.name from pub.student,pub.student_course
where pub.student.sid = pub.student_course.sid and
pub.student_course.cid in
(select cid from pub.student_course
where pub.student_course.sid=’200900130417’);
3.找出至少选修了一门其先行课程号为"300002"号课程的学生的学号、姓名。
create view test2_03 as
select pub.student.sid,pub.student.name from
pub.student,pub.student_course,pub.course
Where pub.student.sid = pub.student_course.sid and
Pub.student_course.cid = pub.course.cid and
Fcid = ‘300002’;
4.找出选修了"操作系统"并且也选修了"数据结构"的学生的学号、姓名。
create view test2_04 as
(select pub.student.sid,pub.student.name from
Pub.student,pub.student_course,pub.course
Where pub.student.sid = pub.student_course.sid and
Pub.student_course.cid = pub.course.cid and
Pub.course.name = ‘操作系统’)
Intersect
(select pub.student.sid,pub.student.name from
Pub.student,pub.student_course,pub.course
Where pub.student.sid = pub.student_course.sid and
Pub.student_course.cid = pub.course.cid and
Pub.course.name = ‘数据结构’);
5.查询20岁的所有有选课的学生的学号、姓名、平均成绩(avg_score,此为列名,下同)(平均成绩四舍五入到个位)、总成绩(sum_score)。
create or replace view test2_05 as select
Pub.student.sid,name,round(avg(score),0) avg_score,sum(score) sum_score
from pub.student,pub.student_course
where pub.student.sid = pub.student_course.sid and pub.student.age = 20
group by pub.student.sid,pub.student.name;
6.查询所有课的最高成绩、次高成绩(次高成绩一定小于最高成绩)、最高成绩人数,test2_06有四个列:课程号cid、课程名称name、最高成绩max_score、次高成绩max_score2、最高成绩人数max_score_count(一个学生同一门课成绩都是第一,只计一次)。如果没有学生选课,则最高成绩为空值,最高成绩人数为零。如果没有次高成绩,则次高成绩为空值。
create or replace view test2_06 as select
a.cid,a.name,max_score,max_score2,max_score_count from
(select cid,name from pub.course) a
left join
(
select sc.cid,count(distinct sid) max_score_count from
pub.student_course sc join
(select cid,max(score) max_score from pub.student_course group by cid) max_sc
on sc.cid = max_sc.cid and sc.score = max_sc.max_score group by sc.cid
) b on a.cid = b.cid
left join
(
select cid,max(score) max_score from pub.student_course group by cid
) c on b.cid = c.cid
left join
(
select two.cid,max(two.score) max_score2 from pub.student_course one join pub.student_course two on one.cid = two.cid and two.score < one.score group by two.cid
) d on c.cid = d.cid;
7.查询所有不姓张、不姓李、也不姓王的学生的学号sid、姓名name。
create or replace view test2_07 as select
sid,name from pub.student where name not like '张%' and name not like '李%' and name not like '王%';
8.查询学生表中每一个姓氏及其人数(不考虑复姓),test2_08有两个列:second_name、p_count。
create or replace view test2_08 as select
b.second_name,count(a.sid) p_count from pub.student a join (select sid,substr(name,1,1) second_name from pub.student) b on a.sid = b.sid group by b.second_name;
9.查询选修了300003号课程的学生的sid、name、score。
create or replace view test2_09 as select
b.sid,b.name,a.score from (select * from pub.student_course where cid = '300003') a join pub.student b
on a.sid = b.sid;
10.找出同一个同学同一门课程有两次或以上不及格的所有学生的学号、姓名(即一门课程需要补考两次或以上的学生的学号、姓名)。
create or replace view test2_10 as select
distinct a.sid,b.name from (select sid,cid,count(*) count from (select * from pub.student_course where score <60) group by sid,cid) a join pub.student b
on a.sid = b.sid where a.count >=2;
1.Oracle与标准SQL的差异需要注意下:
(1)Oracle中没有except关键词,与其等价的是minus关键词。在使用minus时,select语句不能够使用括号,例如select * from student where …… minus select * from student ……,错误写法是(select * from student where ……) minus (select * from student ……);
(2)表别名的定义不能够有As,例如正确的写法select * from student s,错误的写法是 select * from student as s.
2.使用round()实现小数的四舍五入。
3.使用sum(),avg(),count()等聚集函数实现了求和计数取平均等需求。
4.较多的使用到嵌套查询,通过不断地缩小范围找到符合全部要求的数据。
5.使用连接对多个表的内容进行结合,查询到综合数据项。
6.where子句中使用not like和正则表达式进行数据项的判断。
7.实验二有一定的难度,特别是第6小题,我做了很久,每次都有些数据项出错,原因还是考虑的不够仔细。于是我先将各个部分的数据求出来,再进行表的连接。这样的思路会比较简单清晰,缺点就是查询的语句偏长,由多个select子句连接而成。
ps:本人开通了个人的微信公众号,希望大家能关注一下,
我会将资源、文章优先推送到公众号上。
推送自己的一些学习笔记,实验源代码等,
欢迎大家互相交流学习。