转 父表字表统计查询的sql练习
create table father(
f_id number(2) primary key,
f_name varchar2(10)
);
create table son(
s_id number(2) primary key,
s_name varchar2(10),
s_height number(3,2),
s_money number,
f_id number(2),
foreign key(f_id) references father(f_id)
);
--插入父亲信息
insert into father values(1,'何胜达');
insert into father values(2,'何忠达');
insert into father values(3,'何国达');
insert into father values(4,'陌生人');
--插入儿子信息
create sequence s1;
insert into son values(s1.nextval,'何亮',1.70,6000,1);
insert into son values(s1.nextval,'何星',1.68,4000,1);
insert into son values(s1.nextval,'何正安',1.73,7000,2);
insert into son values(s1.nextval,'何正明',1.72,4000,2);
insert into son values(s1.nextval,'何正元',1.68,8500,2);
insert into son values(s1.nextval,'何正陆',1.66,5000,3);
insert into son(s_id,f_id)values(s1.nextval,4);
--查询s_id、s_name、f_id
select s_id,s_name,f_id from son;
--查询各儿子涨价20%以后的新学费,注意,8000块以下的不涨价。
select s_name,s_money*1.2 from son where s_money>8000;
--查询s_id、s_name、f_id、f_name
select f.*,s.s_id,s.s_name
from father f
join son s
on f.f_id=s.f_id;
--查询f_id、f_name、儿子数(没有儿子的不显示)
select f.f_id,f.f_name,count(s.s_name)as 有几个儿子
from father f
join son s
on f.f_id=s.f_id
group by f.f_id,f.f_name
having count(s.s_name)>0;
--查询f_id、f_name、儿子数(没有儿子的个数显示为0)
select f.f_id,f.f_name,count(s.s_name)as 有几个儿子
from father f
join son s
on f.f_id=s.f_id
group by f.f_id,f.f_name
having count(s.s_name) is not null;
--找出不止有1个儿子的father信息:f_id、f_name、儿子数
select f.f_id,f.f_name,count(s.s_name) as 不止一个儿子
from father f
join son s
on f.f_id=s.f_id
group by f.f_id,f.f_name
having count(s.s_name)>1;
--找出儿子最多的father信息:fid、fname、儿子数
select f.f_id,f.f_name,count(s.s_id) as 个数
from father f
join son s
on f.f_id=s.f_id
group by f.f_id,f.f_name;
--、找出fid为(7,9,11)的father中,各儿子的身高
select s_name,s_height from son where f_id in(1,2,3);
--找出所有father中身高最高的儿子。
select s_name from son where s_height=(
select max(s_height) from son
);
--找出各father中身高最高的儿子
select son.* from son,
(select f_id,max(s_height) 最高儿子 from son group by f_id) x
where son.f_id=x.f_id and son.s_height=x.最高儿子;
--找出身高在1.8到1.65之间的所有儿子及父亲信息:fid,fname,sid,sname,height;
--这里用到了where,之所以能用是因为前面没有用到聚合函数
select f.*,s.s_id,s.s_name,s.s_height
from father f
join son s
on f.f_id=s.f_id
where s.s_height between 1.65 and 1.8;
select * from father;
select * from son;
<!DOCTYPE root [
<!ELEMENT root (父亲+,儿子*)>
<!ELEMENT 父亲 EMPTY>
<!ELEMENT 儿子 EMPTY>
<!ATTLIST 父亲
fid CDATA #REQUIRED
姓名 CDATA #REQUIRED
>
<!ATTLIST 儿子
sid CDATA #REQUIRED
姓名 CDATA #REQUIRED
性别 (男|女) #REQUIRED
年龄 CDATA #REQUIRED
学费 CDATA #REQUIRED
身高 CDATA #IMPLIED
fid IDREFS #REQUIRED
>
]>
<root>
<父亲 fid="P_1" 姓名="何胜达"/>
<父亲 fid="P_2" 姓名="何中达"/>
<父亲 fid="P_3" 姓名="何国达"/>
<父亲 fid="P_4" 姓名="陌生人"/>
<儿子 sid="1" 姓名="何亮" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_1"/>
<儿子 sid="1" 姓名="何星" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_1"/>
<儿子 sid="1" 姓名="何正安" 性别="男" 年龄="24" 学费="1000" 身高="1.72" fid="P_2"/>
<儿子 sid="1" 姓名="何明" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_2"/>
<儿子 sid="1" 姓名="何元" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_2"/>
<儿子 sid="1" 姓名="何陆" 性别="男" 年龄="24" 学费="5000" 身高="1.72" fid="P_3"/>
</root>
转 父表字表统计查询的sql练习的更多相关文章
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
- mysql按年度、季度、月度、周、日统计查询的sql语句
本文介绍一些mysql中用于查询的sql语句,包括按年度.季度.月度.周.日统计查询等,有需要的朋友,可以参考下. 一.年度查询 查询 本年度的数据 SELECT * FROM blog_arti ...
- thinkphp区间查询、统计查询、SQL直接查询
区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是(and)并且的关系 //SELECT * FROM `tp_user` WHE ...
- 175. Combine Two Tables【LeetCode】-LEFT JON 和RIGHT JOIN,两张表关联查询-java -sql入门
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- 数据库联表统计查询 Group by & INNER JOIN
原数据表 视频信息表 tab_video_info 播放记录表 tab_play_record 需求 统计播放量(已经开始播放)最多的前20个视频: SELECT a.video_id, SUM( ...
- ACTION 关联表之间查询语句 SQL语句写法
/** EquUseRecord * @author cll * @return * @右边菜单中的使用记录操作 */ public String QueryAllEquUserecordAllInf ...
- hibernate实现多表联合查询
转自:http://blog.sina.com.cn/s/blog_67b9ad8d01010by1.html 以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hiber ...
- hibernate 多表联合查询
以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hibernate要实现多表联合查询的时候还楞了一下.最后看了下资料,才恍然大悟,hibernate实现多表联合查询跟SQ ...
- SQL调优--记一次表统计信息未及时更新导致查询超级慢
某日同事丢给我一个看上去复杂的查询(实际就涉及两张表,套来套去)说只是换了日期条件,但一个查询5秒出数据,一个根本查不出来.现在整理下解决过程,及涉及的知识点. 若有不正之处, ...
随机推荐
- day6-3面向对象高阶
面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...
- Bootstrap 4 中 Alerts 实现
Alert 的使用说明 http://v4-alpha.getbootstrap.com/components/alerts/ JavaScript behavior Triggers Enable ...
- TortoiseSVN 版本回滚
尝试用TortoiseSVN进行版本回滚,回滚到的版本和实际的内容有出入,可能是点了太多次给点乱了,囧~ 不过发现一个比较靠谱的方法,如下: 右键点击文件TortoiseSVN->showlog ...
- mybatis-generator-core生成代码
mybatis-generator-core-1.3.3下载地址:http://blog.mybatis.org/p/products.html 下载后名解压,进入lib目录 修改一个Generato ...
- django(五)
URLs 当一个用户请求一个页面时,Django将按照顺序去匹配每一个模式,并停在第一个匹配请求的URL上. 如果你的url多个正则表达式都能匹配上咋弄?小心出错,这个是按照顺序匹配的 url(r'^ ...
- 初始化成员列表 ——— 类的const成员和引用成员的初始化
class A { public: A(){}; const int num; CString& s; } A::A() { cout<<A con<<endl; } ...
- 7,SFDC 管理员篇 - 数据模型 - 公式和验证 1
1,自定义公式 Customize | Your Object | Fields | Add Fields Field SF的公式和Excel的公式差不多,都是支持各种运算和结果 例1,以opport ...
- nginx的一些介绍和使用
nginx 的安装 我们首先进行下载安装:http://nginx.org/download/nginx-1.4.2.tar.gz 安装准备: nginx依赖于pcre库,要先安装pcre 1 yum ...
- TJI读书笔记12-接口
TJI读书笔记12-接口 抽象类和抽象方法 接口 完全解耦和策略模式 接口间的继承关系 工厂模式 乱七八糟不知道怎么归类的知识点 接口和抽象类为我们提供了更强又有力的接口和实现分离的方法. 抽象类和抽 ...
- SVM1 线性SVM
一.Linear Support Vector Machine 接下来的讨论假设数据都是线性可分的. 1.1 SVM的引入:增大对测量误差的容忍度 假设有训练数据和分类曲线如下图所示: 很明显,三个分 ...