Orcle数据库查询练习复习:一
一、创建数据库和表
drop table student; create table student ( sid int, sname varchar2(100), sage int, ssex char(3), snativeplace varchar2(100), smajor varchar2(100), sclass varchar2(100), snative varchar2(100) ); drop table course; create table course ( cid int, cname varchar2(100), cval int, ctime int, tid int ); drop table mark; create table mark ( sid int, cid int, cmark int ); drop table teacher; create table teacher ( tid int, tname varchar2(100), tsex char(3), tage int, tlvl varchar2(100) ); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10001,'廖官忠',21,'男','福建','信计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10002,'张三',20,'男','江苏','信计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10003,'李四',19,'男','福建','信计','2班','傣族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10004,'萧瑾',21,'女','福建','统计','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10005,'叶晶',21,'女','上海','传煤','1班','朝鲜族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10006,'萧瑾',19,'女','北京','统计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10007,'李宁',21,'男','云南','信控','1班','傣族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10008,'唐品',18,'男','江苏','信计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10009,'吴强',20,'男','山东','统计','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10010,'欧阳锋',22,'男','四川','应数','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10011,'王充样',23,'男','黑龙江','统计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10012,'卫小宝',17,'女','福建','统计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10013,'李绍',21,'女','福建','信计','2班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10014,'黄马华',22,'男','浙江','心理学','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10015,'艾蔚儿',19,'女','福建','计算机','1班','黎族'); commit; insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10016,'赵若辰',21,'男','江苏','心理学','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10017,'徐扬',22,'男','青海','计算机','1班','汉族'); insert into student (sid,sname,sage,ssex,snativeplace,smajor,sclass,snative) values (10018,'徐静静',19,'女','安徽','计算机','1班','黎族'); insert into course (cid,cname,cval,ctime,tid) values (2001,'数学',6,64,30001); insert into course (cid,cname,cval,ctime,tid) values (2002,'英语',4,64,30002); insert into course (cid,cname,cval,ctime,tid) values (2003,'体育',2,32,30003); insert into course (cid,cname,cval,ctime,tid) values (2004,'马克思主义',6,64,30004); insert into course (cid,cname,cval,ctime,tid) values (2005,'计算机基础',3,48,30005); insert into course (cid,cname,cval,ctime,tid) values (2006,'心理学',4,48,30006); insert into course (cid,cname,cval,ctime,tid) values (2007,'空间天气学',6,64,30007); insert into teacher (tid,tname,tsex,tage,tlvl) values (30001,'马六','男',45,'高级教师'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30002,'胡美丽','女',32,'中级教师'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30003,'李强','男',40,'高级教师'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30004,'胡适','男',55,'教授'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30005,'钱枫','男',37,'高级教师'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30006,'戴安安','女',27,'中级教师'); insert into teacher (tid,tname,tsex,tage,tlvl) values (30007,'张伯伦','男',47,'教授'); drop table teacher create table teacher ( tid int, tname varchar2(100), tsex char(2), tage int, tlvl varchar2(100) ) insert into mark (sid,cid,cmark) values (10001,2001,85); insert into mark (sid,cid,cmark) values (10001,2002,75); insert into mark (sid,cid,cmark) values (10001,2003,80); insert into mark (sid,cid,cmark) values (10001,2004,70); insert into mark (sid,cid,cmark) values (10001,2005,60); insert into mark (sid,cid,cmark) values (10001,2006,95); insert into mark (sid,cid,cmark) values (10001,2007,70); insert into mark (sid,cid,cmark) values (10002,2001,80); insert into mark (sid,cid,cmark) values (10002,2002,65); insert into mark (sid,cid,cmark) values (10002,2003,70); insert into mark (sid,cid,cmark) values (10002,2004,80); insert into mark (sid,cid,cmark) values (10002,2005,55); insert into mark (sid,cid,cmark) values (10002,2006,78); insert into mark (sid,cid,cmark) values (10002,2007,82); insert into mark (sid,cid,cmark) values (10003,2001,69); insert into mark (sid,cid,cmark) values (10003,2002,57); insert into mark (sid,cid,cmark) values (10003,2003,90); insert into mark (sid,cid,cmark) values (10003,2004,80); insert into mark (sid,cid,cmark) values (10003,2005,77); insert into mark (sid,cid,cmark) values (10003,2006,92); insert into mark (sid,cid,cmark) values (10003,2007,80); insert into mark (sid,cid,cmark) values (10004,2001,85); insert into mark (sid,cid,cmark) values (10004,2002,76); insert into mark (sid,cid,cmark) values (10004,2003,66); insert into mark (sid,cid,cmark) values (10004,2004,54); insert into mark (sid,cid,cmark) values (10004,2005,80); insert into mark (sid,cid,cmark) values (10004,2006,73); insert into mark (sid,cid,cmark) values (10004,2007,80); insert into mark (sid,cid,cmark) values (10005,2001,93); insert into mark (sid,cid,cmark) values (10005,2002,82); insert into mark (sid,cid,cmark) values (10005,2003,71); insert into mark (sid,cid,cmark) values (10005,2004,68); insert into mark (sid,cid,cmark) values (10005,2005,70); insert into mark (sid,cid,cmark) values (10005,2006,86); insert into mark (sid,cid,cmark) values (10005,2007,90); insert into mark (sid,cid,cmark) values (10006,2001,69); insert into mark (sid,cid,cmark) values (10006,2002,48); insert into mark (sid,cid,cmark) values (10006,2003,90); insert into mark (sid,cid,cmark) values (10006,2004,68); insert into mark (sid,cid,cmark) values (10006,2005,80); insert into mark (sid,cid,cmark) values (10006,2006,88); insert into mark (sid,cid,cmark) values (10006,2007,70); insert into mark (sid,cid,cmark) values (10007,2001,77); insert into mark (sid,cid,cmark) values (10007,2002,75); insert into mark (sid,cid,cmark) values (10007,2003,82); insert into mark (sid,cid,cmark) values (10007,2004,67); insert into mark (sid,cid,cmark) values (10007,2005,84); insert into mark (sid,cid,cmark) values (10007,2006,95); insert into mark (sid,cid,cmark) values (10007,2007,73); insert into mark (sid,cid,cmark) values (10008,2001,97); insert into mark (sid,cid,cmark) values (10008,2002,86); insert into mark (sid,cid,cmark) values (10008,2003,68); insert into mark (sid,cid,cmark) values (10008,2004,81); insert into mark (sid,cid,cmark) values (10008,2005,71); insert into mark (sid,cid,cmark) values (10008,2006,78); insert into mark (sid,cid,cmark) values (10008,2007,64); insert into mark (sid,cid,cmark) values (10009,2001,90); insert into mark (sid,cid,cmark) values (10009,2002,75); insert into mark (sid,cid,cmark) values (10009,2003,60); insert into mark (sid,cid,cmark) values (10009,2004,57); insert into mark (sid,cid,cmark) values (10009,2005,90); insert into mark (sid,cid,cmark) values (10009,2006,85); insert into mark (sid,cid,cmark) values (10009,2007,91); insert into mark (sid,cid,cmark) values (10010,2001,85); insert into mark (sid,cid,cmark) values (10010,2002,62); insert into mark (sid,cid,cmark) values (10010,2003,72); insert into mark (sid,cid,cmark) values (10010,2004,81); insert into mark (sid,cid,cmark) values (10010,2005,91); insert into mark (sid,cid,cmark) values (10010,2006,90); insert into mark (sid,cid,cmark) values (10010,2007,66); insert into mark (sid,cid,cmark) values (10011,2001,55); insert into mark (sid,cid,cmark) values (10011,2002,75); insert into mark (sid,cid,cmark) values (10011,2003,67); insert into mark (sid,cid,cmark) values (10011,2004,87); insert into mark (sid,cid,cmark) values (10011,2005,88); insert into mark (sid,cid,cmark) values (10011,2006,77); insert into mark (sid,cid,cmark) values (10011,2007,61); insert into mark (sid,cid,cmark) values (10012,2001,77); insert into mark (sid,cid,cmark) values (10012,2002,81); insert into mark (sid,cid,cmark) values (10012,2003,91); insert into mark (sid,cid,cmark) values (10012,2004,67); insert into mark (sid,cid,cmark) values (10012,2005,60); insert into mark (sid,cid,cmark) values (10012,2006,80); insert into mark (sid,cid,cmark) values (10012,2007,74); insert into mark (sid,cid,cmark) values (10013,2001,88); insert into mark (sid,cid,cmark) values (10013,2002,75); insert into mark (sid,cid,cmark) values (10013,2003,90); insert into mark (sid,cid,cmark) values (10013,2004,60); insert into mark (sid,cid,cmark) values (10013,2005,71); insert into mark (sid,cid,cmark) values (10013,2006,95); insert into mark (sid,cid,cmark) values (10013,2007,89); insert into mark (sid,cid,cmark) values (10014,2001,65); insert into mark (sid,cid,cmark) values (10014,2002,72); insert into mark (sid,cid,cmark) values (10014,2003,68); insert into mark (sid,cid,cmark) values (10014,2004,67); insert into mark (sid,cid,cmark) values (10014,2005,60); insert into mark (sid,cid,cmark) values (10014,2006,75); insert into mark (sid,cid,cmark) values (10014,2007,81); insert into mark (sid,cid,cmark) values (10015,2001,55); insert into mark (sid,cid,cmark) values (10015,2002,67); insert into mark (sid,cid,cmark) values (10015,2003,61); insert into mark (sid,cid,cmark) values (10015,2004,71); insert into mark (sid,cid,cmark) values (10015,2005,81); insert into mark (sid,cid,cmark) values (10015,2006,88); insert into mark (sid,cid,cmark) values (10015,2007,90); insert into mark (sid,cid,cmark) values (10016,2001,72); insert into mark (sid,cid,cmark) values (10016,2002,82); insert into mark (sid,cid,cmark) values (10016,2003,92); insert into mark (sid,cid,cmark) values (10016,2004,62); insert into mark (sid,cid,cmark) values (10016,2005,63); insert into mark (sid,cid,cmark) values (10016,2006,73); insert into mark (sid,cid,cmark) values (10016,2007,83); insert into mark (sid,cid,cmark) values (10017,2001,85); insert into mark (sid,cid,cmark) values (10017,2002,77); insert into mark (sid,cid,cmark) values (10017,2003,87); insert into mark (sid,cid,cmark) values (10017,2004,79); insert into mark (sid,cid,cmark) values (10017,2005,69); insert into mark (sid,cid,cmark) values (10017,2006,58); insert into mark (sid,cid,cmark) values (10017,2007,74); insert into mark (sid,cid,cmark) values (10018,2001,88); insert into mark (sid,cid,cmark) values (10018,2002,78); insert into mark (sid,cid,cmark) values (10018,2003,88); insert into mark (sid,cid,cmark) values (10018,2004,78); insert into mark (sid,cid,cmark) values (10018,2005,68); insert into mark (sid,cid,cmark) values (10018,2006,98); insert into mark (sid,cid,cmark) values (10018,2007,78); commit;
二、题目
1.在学生表中找出人数至少是三个的地区
select snativeplace from student group by snativeplace having count(*)>=3 /*having扫描的是每个小组*/
2.找出各地区女生年龄数目
select snativeplace,count(distinct sage) from student where ssex='女' group by snativeplace
3.找出各个地区女生的数学平均分
select snativeplace ,avg(cmark) from student,mark,course where student.sid=mark.sid and mark.cid=course.cid and ssex='女' and cname='数学' group by snativeplace order by avg(cmark)
4.找出各个地区各门课的均分
select snativeplace ,cname,avg(cmark) from student s,mark sc,course c where s.sid=sc.sid and sc.cid=c.cid group by snativeplace ,c.cid,cname
5.找出低于所有人平均分的学生姓名
select sname from student where sid in(select sid from mark group by sid having avg(cmark)<(select avg(cmark)from mark))
6.找出年龄降序排序的3-6名学生信息
select * from(select * from( select * from student order by sage desc)where rownum<=7) order by sage select *from( select * from(select * from( select * from student order by sage desc)where rownum<=7 ) order by sage ) where rownum<=5 order by sage desc
7.找出年龄最大学生
方法1:排序取第一条 select * from( select * from student order by sage desc) t where rownum=1 访法2:找到最大值,其次扫描那些记录的数值等于这个最大值 step1:select max(sage) from student step2:select* from student where sage=(step1) 方法三:直接和原表的所有记录进行对比,大于等于他们所有记录则成立 select * from student where sage>=all(select sage from student) 方法4:排除法 select * from student where not sage<any(select sage from student)
8.找出均分最高的学生姓名
select*from( select mark.sid,sname,avg(cmark) from mark,student where mark.sid=student.sid group by mark.sid,sname order by avg(cmark) desc ) where rownum=1 select * from student where sid in(select sid from mark group by sid having avg(cmark)>=all(select avg(cmark) from mark group by sid))
9.找出均分最高的女生姓名
step1:select sid from student where ssex='女' /*找出女生学号*/ step2:select sid avg(cmark)amk from mark where sid in (step1)group by sid/*计算出女生均分表*/ step3:select sid from(step2)where amk>=all(select amk from step2)/*使用>=all的方式找到最高均分的学号*/ step4:select snme from student where sid in(step3)/*在学生表中兑换成姓名*/
10.找出数学成绩最高的学生姓名
step1: select sid from student where ssex='女' step2: select cid from course where canme='数学' step3: select * from mark where sid in(step1) and cid=(step2) step4: select max(cmark) from (step3) step5:select sid from (step3) where cmark =(step4) step5:select sname from student where sid in(step5)
本文来自博客园,作者:HuTiger,转载请注明原文链接:https://www.cnblogs.com/huguodong/p/5846577.html,欢迎加入qq群927460240学习讨论