刚学了Oracle数据库的SQL,DDL,DML,DCL等语句,然后在网上搜到的一套题,但是,这套题的答案有些和我理解的题目需求不同,这里的答案是我自己根据自己理解的需求写出来的,欢迎各位看客指点不足。
---------------------------------------------------------------------------------------------------------
-----建表
1 create table student( 2 sno varchar2(10) primary key, 3 sname varchar2(20), 4 sage number(2), 5 ssex varchar2(5) 6 ); 7 create table teacher( 8 tno varchar2(10) primary key, 9 tname varchar2(20) 10 ); 11 create table course( 12 cno varchar2(10), 13 cname varchar2(20), 14 tno varchar2(20), 15 constraint pk_course primary key (cno,tno) 16 ); 17 create table sc( 18 sno varchar2(10), 19 cno varchar2(10), 20 score number(4,2), 21 constraint pk_sc primary key (sno,cno) 22 );
-----插入数据
1 /*******初始化学生表的数据**********/ 2 insert into student values ('s001','张三',23,'男'); 3 insert into student values ('s002','李四',23,'男'); 4 insert into student values ('s003','吴鹏',25,'男'); 5 insert into student values ('s004','琴沁',20,'女'); 6 insert into student values ('s005','王丽',20,'女'); 7 insert into student values ('s006','李波',21,'男'); 8 insert into student values ('s007','刘玉',21,'男'); 9 insert into student values ('s008','萧蓉',21,'女'); 10 insert into student values ('s009','陈萧晓',23,'女'); 11 insert into student values ('s010','陈美',22,'女'); 12 insert into student values ('s011','陈美',23,'女'); 13 commit; 14 /******************初始化教师表***********************/ 15 insert into teacher values ('t001', '刘阳'); 16 insert into teacher values ('t002', '谌燕'); 17 insert into teacher values ('t003', '胡明星'); 18 commit; 19 /***************初始化课程表****************************/ 20 insert into course values ('c001','J2SE','t002'); 21 insert into course values ('c002','Java Web','t002'); 22 insert into course values ('c003','SSH','t001'); 23 insert into course values ('c004','Oracle','t001'); 24 insert into course values ('c005','SQL SERVER 2005','t003'); 25 insert into course values ('c006','C#','t003'); 26 insert into course values ('c007','JavaScript','t002'); 27 insert into course values ('c008','DIV+CSS','t001'); 28 insert into course values ('c009','PHP','t003'); 29 insert into course values ('c010','EJB3.0','t002'); 30 commit; 31 /***************初始化成绩表***********************/ 32 insert into sc values ('s001','c001',78.9); 33 insert into sc values ('s002','c001',80.9); 34 insert into sc values ('s003','c001',81.9); 35 insert into sc values ('s004','c001',60.9); 36 insert into sc values ('s001','c002',82.9); 37 insert into sc values ('s002','c002',72.9); 38 insert into sc values ('s003','c002',81.9); 39 insert into sc values ('s001','c003','59'); 40 insert into sc values ('s001','c007','90'); 41 insert into sc values ('s001','c010','90'); 42 insert into sc values ('s002','c007',72.9); 43 insert into sc values ('s002','c010',72.9); 44 insert into sc values ('s002','c003',72.9); 45 insert into sc values ('s006','c003',76); 46 insert into sc values ('s001','c005','58'); 47 commit;
-----题目
- 查询“c001”课程比“c002”课程成绩高的所有学生的学号;
- 查询平均成绩大于60 分的同学的学号和平均成绩;
- 查询所有同学的学号、姓名、选课数、总成绩;
- 查询姓“刘”的老师的个数;
- 查询没学过“谌燕”老师课的同学的学号、姓名;
- 查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
- 查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
- 查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
- 查询所有课程成绩小于60 分的同学的学号、姓名;
- 查询没有学全所有课的同学的学号、姓名;
- 查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
- 查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;
- 把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
- 查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;
- 删除学习“谌燕”老师课的SC 表记录;
- 向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
- 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
- 按各科平均成绩从低到高和及格率的百分数从高到低顺序
- 查询不同老师所教不同课程平均分从高到低显示
- 统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
- 查询各科成绩前三名的记录:(不考虑成绩并列情况)
- 查询每门课程被选修的学生数
- 查询出只选修了一门课程的全部学生的学号和姓名
- 查询男生、女生人数
- 查询姓“张”的学生名单
- 查询同名同性学生名单,并统计同名人数
- 1995 年出生的学生名单(注:Student 表中Sage 列的类型是number)
- 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
- 查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
- 查询课程名称为“数据库”,且分数低于60 的学生姓名和分数
- 查询所有学生的选课情况;
- 查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
- 查询不及格的课程,并按课程号从大到小排列
- 查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
- 求选了课程的学生人数
- 查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
- 查询各个课程及相应的选修人数
- 查询不同课程成绩相同的学生的学号、课程号、学生成绩
- 查询每门功课成绩最好的前两名
- 统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
- 检索至少选修两门课程的学生学号
- 查询全部学生都选修的课程的课程号和课程名
- 查询没学过“谌燕”老师讲授的任一门课程的学生姓名
- 查询两门以上不及格课程的同学的学号及其平均成绩
- 检索“c004”课程分数小于60,按分数降序排列的同学学号
- 删除“s002”同学的“c001”课程的成绩
-----答案
1 --1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; 2 select s1.sno 3 from sc s1 4 join sc s2 on s1.sno = s2.sno 5 where s1.cno = 'c001' 6 and s2.cno='c002' 7 and s1.score>s2.score; 8 9 --2、查询平均成绩大于60 分的同学的学号和平均成绩; 10 select sno,avg(score) 11 from sc 12 group by sno 13 having avg(score)>60; 14 15 --3、查询所有同学的学号、姓名、选课数、总成绩; 16 select s2.sno,s1.sname,count(*),sum(score) 17 from student s1,sc s2 18 where s1.sno = s2.sno 19 group by s2.sno,s1.sname; 20 21 --4、查询姓“刘”的老师的个数; 22 select count(*) 23 from teacher 24 where tname like '刘%'; 25 26 --5、查询没学过“谌燕”老师课的同学的学号、姓名; 27 select s1.sno,s1.sname 28 from student s1 29 where s1.sno not in (--得到学习这个老师课程的学生 30 select distinct s2.sno 31 from sc s2 32 where s2.cno in (--得到这个老师的课程 33 select c.cno 34 from course c 35 where c.tno = (--得到这个老师的tno 36 select tno 37 from teacher 38 where tname ='谌燕'))); 39 40 --6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名; 41 select s2.sno,s1.sname 42 from student s1,sc s2,sc s3 43 where s1.sno = s2.sno 44 and s2.cno = 'c002' 45 and s2.sno = s3.sno 46 and s3.cno = 'c001'; 47 48 --7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名; 49 select s1.sno,s1.sname from student s1 50 where not exists ( 51 select * 52 from (select cno from course--查询得到这个老师的所有课程 53 where tno = (--查询得到这个老师的tno 54 select tno from teacher 55 where tname = '谌燕')) c 56 where not exists ( 57 select * from sc s2 58 where s2.sno = s1.sno 59 and s2.cno = c.cno)) 60 61 --8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名; 62 select sno,sname 63 from student 64 where sno in ( 65 select s1.sno 66 from sc s1,sc s2 67 where s1.sno= s2.sno 68 and s1.cno = 'c001' 69 and s2.cno = 'c002' 70 and s1.score>s2.score 71 ) 72 73 --9、查询所有 课程成绩小于60 分的同学的学号、姓名; 74 select sno,sname 75 from student 76 where sno in ( 77 select sno 78 from sc s2 79 where s2.score<60); 80 /************第二种*******************/ 81 select s2.sno,s1.sname---更优化 82 from student s1 83 join sc s2 on s1.sno = s2.sno 84 where s2.score<60; 85 86 --10、查询没有学全所有课的同学的学号、姓名; 87 select sno,sname 88 from student 89 where sno not in ( 90 select sno 91 from sc 92 group by sno 93 having count(*) >= ( 94 select count(distinct cno) 95 from course) 96 ) order by sno 97 98 --11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名; 99 select distinct s2.sno,s1.sname 100 from student s1 101 join sc s2 102 on s1.sno = s2.sno 103 where s2.cno in ( 104 select cno 105 from sc 106 where sno = 's001') 107 108 --12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名; 109 select distinct s2.sno,s1.sname 110 from student s1 111 join sc s2 112 on s1.sno = s2.sno 113 where s2.sno <> 's001' 114 and s2.cno in ( 115 select cno 116 from sc 117 where sno = 's001') 118 119 --13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩; 120 update sc s1 121 set score = ( 122 select round(avg(s2.score),1) 123 from sc s2 124 where s2.cno = s1.cno) 125 where cno in ( 126 select cno 127 from course 128 where tno = ( 129 select tno 130 from teacher 131 where tname='谌燕')); 132 133 --14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名; 134 select s1.sno,s1.sname 135 from student s1 136 where not exists( 137 select * 138 from (select cno 139 from sc 140 where sno = 's001') c 141 where not exists( 142 select * 143 from sc s2 144 where s1.sno = s2.sno 145 and s2.cno = c.cno 146 and s2.sno <> 's001' 147 ) 148 ) 149 150 --15、删除学习“谌燕”老师课的SC 表记录; 151 delete sc 152 where cno in ( 153 select cno 154 from course 155 where tno = ( 156 select tno 157 from teacher 158 where tname='谌燕')); 159 160 --16、向SC 表中 插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩; 161 insert into sc (sno,cno,score) 162 select distinct st.sno,sc.cno,( 163 select avg(score) 164 from sc 165 where cno='c002') 166 from student st,sc 167 where not exists 168 (select * 169 from sc 170 where cno='c002' 171 and sc.sno=st.sno) 172 and sc.cno='c002'; 173 174 commit; 175 176 --17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 177 select cno,max(score),min(score) 178 from sc 179 group by cno; 180 181 --18、按各科平均成绩从低到高和及格率的百分数从高到低顺序 182 select cno,avg(score), 183 sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率 184 from sc 185 group by cno 186 order by avg(score) , 及格率 desc 187 188 --19、查询不同老师所教不同课程平均分从高到低显示 189 select s.cno,avg(s.score) 190 from course c 191 join sc s on c.cno = s.cno 192 group by c.tno,s.cno 193 order by avg(score) desc--MY 194 /***********第二种**************/ 195 select max(t.tno),max(t.tname),max(c.cno),max(c.cname),c.cno,avg(score) 196 from sc , course c,teacher t 197 where sc.cno=c.cno and c.tno=t.tno 198 group by c.cno 199 order by avg(score) desc 200 201 --20、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 202 select s.cno,max(c.cname), 203 sum(case when s.score between 85 and 100 then 1 else 0 end) as "[100-85]", 204 sum(case when s.score between 70 and 84 then 1 else 0 end) as "[85-70]", 205 sum(case when s.score between 60 and 69 then 1 else 0 end) as "[70-60]", 206 sum(case when s.score <60 then 1 else 0 end) as "[ <60]" 207 from course c 208 join sc s on s.cno = c.cno 209 group by s.cno 210 211 --21、查询各科成绩前三名的记录:(不考虑成绩并列情况) 212 select * from 213 (select sno,cno,score,row_number()over(partition by cno order by score desc) rn 214 from sc) 215 where rn<4 216 217 --22、查询每门课程被选修的学生数 218 select distinct c.cno,count(s.sno) --显示不出现sc表中的课程 219 from course c 220 left join sc s on c.cno = s.cno 221 group by c.cno; 222 223 select cno,count(sno) 224 from sc 225 group by cno;--只显示sc表中的课程 226 227 --23、查询出只选修了一门课程的全部学生的学号和姓名 228 select sno,sname 229 from student 230 where sno in ( 231 select s2.sno 232 from sc s2 233 group by s2.sno 234 having count(s2.cno) = 1) 235 236 --24、查询男生、女生人数 237 select ssex,count(*) 238 from student 239 group by ssex 240 241 --25、查询姓“张”的学生名单 242 select * 243 from student 244 where sname like '张%' 245 246 --26、查询同名同姓学生名单,并统计同名人数 247 select sname,count(*) 248 from student 249 group by sname 250 having count(*)>1 251 252 --27、1995 年出生的学生名单(注:Student 表中Sage 列的类型是number) 253 select * 254 from student 255 where to_char(sysdate,'yyyy')-sage = 1995--得到当前时间的年份,减去年龄等于1995 256 257 --28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 258 select cno,round(avg(score),1) 259 from sc 260 group by cno 261 order by avg(score),cno desc 262 263 --29、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩 264 select s2.sno,s1.sname,avg(s2.score) 265 from sc s2 266 join student s1 on s1.sno = s2.sno 267 group by s1.sname,s2.sno 268 having avg(s2.score)>80 269 270 --30、查询课程名称为“Oracle”,且分数低于60 的学生姓名和分数 271 select s1.sname,s2.score 272 from student s1,sc s2,course c 273 where s1.sno = s2.sno 274 and s2.cno = c.cno 275 and c.cname='Oracle' 276 and s2.score<60 277 278 --31、查询所有学生的选课情况; 279 select st.sno,st.sname,c.cname 280 from student st,sc,course c 281 where sc.sno(+)=st.sno 282 and sc.cno=c.cno(+); 283 284 --32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数; 285 select st.sname,s.score,c.cname 286 from student st,sc s,course c 287 where st.sno = s.sno 288 and s.cno = c.cno 289 and s.score >70 290 291 --33、查询不及格的课程,并按课程号从大到小排列 292 select * 293 from sc 294 where score < 60 295 order by cno desc 296 297 --34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名; 298 select s.sno,st.sname,s.cno,s.score 299 from student st,sc s 300 where st.sno = s.sno 301 and s.cno = 'c001' 302 and s.score > 80 303 304 --35、求选了课程的学生人数 305 select count(distinct sc.sno) 选课人数 306 from sc 307 308 --36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩 309 select max(st.sname),max(s.score)------cost:10 310 from student st,sc s,course c,teacher t 311 where st.sno = s.sno 312 and s.cno = c.cno 313 and c.tno = t.tno 314 and t.tname = '谌燕'; 315 /************第二种********************/ 316 select max(st.sname),max(s.score)----cost:9 317 from student st 318 join sc s using(sno) 319 where cno in ( 320 select cno 321 from course 322 where tno = ( 323 select tno 324 from teacher 325 where tname='谌燕')); 326 327 --37、查询各个课程及相应的选修人数 328 select cno,count(*)----sc表中的课程 329 from sc 330 group by cno 331 order by cno 332 /*******************************/ 333 select c.cno,count(s.sno)---course表中的课程 334 from sc s,course c 335 where s.cno(+) = c.cno 336 group by c.cno 337 338 --38、查询不同课程成绩相同的学生的学号、课程号、学生成绩 339 select * from 340 sc s1,sc s2 341 where s1.score = s2.score 342 and s1.cno <> s2.cno 343 344 --39、查询每门功课成绩最好的前两名 345 select * 346 from (select sno,cno,score, 347 row_number()over(partition by cno order by score desc) rn 348 from sc) 349 where rn <3 350 351 --40、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 352 select cno,count(sno) 353 from sc 354 group by cno 355 having count(sno) > 10 356 order by count(sno) desc,cno 357 358 --41、检索至少选修两门课程的学生学号 359 select sno,count(cno) 360 from sc 361 group by sno 362 having count(cno) > 1 363 364 --42、查询全部学生都选修的课程的课程号和课程 365 select cno,cname 366 from course 367 where cno in (--得到满足条件的课程号 368 select cno 369 from sc 370 group by cno 371 having count(sno) = (--如果这个课程的人数等于sc表中学生的人数,说明此课程满足条件 372 select count(distinct sno)--得到sc表中学生的人数 373 from sc)) 374 375 --43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名 376 select sname 377 from student 378 where sno not in(--输出没有选修的学生 379 select distinct sc.sno--找到选修这个老师的学生 380 from sc,course c,teacher t 381 where sc.cno=c.cno 382 and c.tno=t.tno 383 and t.tname='谌燕' 384 ); 385 386 --44、查询两门以上不及格课程的同学的学号及其平均成绩 387 select s1.sno,avg(s1.score) 388 from sc s1 389 where (select count(*)--不及格课程的门数 390 from sc s2 391 where s2.sno = s1.sno 392 and s2.score < 60) >1--两门以上,包括2门 393 group by s1.sno 394 395 --45、检索“c004”课程分数小于60,按分数降序排列的同学学号 396 select sno 397 from sc 398 where cno = 'c004' 399 and score < 60 400 order by score desc 401 402 --46、删除“s002”同学的“c001”课程的成绩 403 delete sc 404 where sno = 's002' 405 and cno = 'c001'
------总结