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)
Orcle数据库查询练习复习:一的更多相关文章
- Orcle数据库查询练习复习:四
一.题目 1.找出张三的最高分和最低分以及对应的课程名 select * from course c,mark m where c.cid=m.cid and sid =(select sid fro ...
- Orcle数据库查询练习复习:三
一.题目 1.与“张三”同乡的男生姓名 select * from student where snativeplace=(select snativeplace from student where ...
- Orcle数据库查询练习复习:二
一.题目 1.找出所有成绩均低于80的学生姓名 select sname from student where sid in( ) select sname from student where si ...
- 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。
最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...
- [转]C#反射,根据反射将数据库查询数据和实体类绑定,并未实体类赋值
本文来自:http://www.cnblogs.com/mrchenzh/archive/2010/05/31/1747937.html /****************************** ...
- 各数据库查询前N条记录的SQL语句
sql在不同数据库查询前几条数据 1. ORACLE SELECT * FROM TABLE_NAME WHERE ROWNUM <= N; HQL: from table_name t or ...
- Atitit DbServiceV4qb9 数据库查询类库v4 新特性
Atitit DbServiceV4qb9 数据库查询类库v4 新特性 V4新特性 安全特性,屏蔽了executeUpdate,使用v2版 Sql异常转换,特别转换了DuplicateEnt ...
- C#与mysql做ASP.NET网页数据库查询速度测试
两种方法是:1,使用mysql数据库的存储过程:2,C#编码,做网页后台与mysql数据库连接,前台测试显示测试过结果下面我将分别讲解两种方法的具体实现. 1,使用mysql数据库的存储过程插入万条大 ...
- 第九十九天上课 PHP TP框架 数据库查询和增加
在Model文件夹下创建模型,文件命名规则 : 表名Model.class.php <?php namespace Home\Model; use Think\Model; class yong ...
随机推荐
- 使用VisualSVN Server自动发布站点
使用VisualSVN Server自动发布站点可以节省大量的发布时间. 适用于项目测试版本 通常一个项目在测试阶段会遇到以下问题 1.开发人员修改缺陷要实时反应到测试环境 2.项目经理想让客户及时看 ...
- 基于 WebAPI 的 API 实现
本文基于 WebAPI OData (微软发起的一个格式标准,其中一个比较有意思的是可以直接在 Excel 中填入 API 就可以展示了) Swashbuckle.OData(把 API 生成一个测试 ...
- c语言之fopen参数(r+,w+,a+)
经查找一些资料,并且亲自实验之后得出: r+:打开已存在的文件时,光标位于开头,文件可读,可写, 写数据时,由于光标位于开头,所以会覆盖原有数据 w+:打开的文件已存在时,将覆盖原文件 a+:打开已存 ...
- linux c 分解质因数
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...
- ActiveMQ之jmscorrelationid与selector
前面讲过JMSCorrelationID主要是用来关联多个Message,例如需要回复一个消息的时候,通常把回复的消息的JMSCorrelationID设置为原来消息的ID.在下面这个例子中,创建了三 ...
- (菜鸟要飞系列)一,基于Asp.Net MVC5的后台管理系统(前言)
今天真是个郁闷的日子,因为老师两个星期前给我的一个任务,用递归算法将Oracle数据库中用户信息及权限显示在jquery-treeView上,网上虽然有大神写出了这类算法,但是不贴全部代码,真的很难跟 ...
- 在Visual Studio 2010 中创建类库(dll)
创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...
- MVC中的几个问题汇总
1.The model backing the 'XXXXDBContext' context has changed since the database was created. Either m ...
- Asp.Net生命周期系列二
在上回书开始的时候我们提到博客园的IIS看了一眼我的请求后就直接交给ASP.NET去处理了,并且要求ASP.NET处理完之后返回HTML以供展示. 那么我们不仅要问: 1, IIS肯定是没有眼睛 ...
- SQL SERVER 強制指定使用索引 -转载 只为学习
今天很高兴 ,有学会了一种数据库优化的方式,哈哈 今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB', ...