转 父表字表统计查询的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秒出数据,一个根本查不出来.现在整理下解决过程,及涉及的知识点. 若有不正之处, ...
随机推荐
- 关于@property()的那些属性及ARC简介
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- KMeans的图像压缩
# -*- coding: utf-8 -*- """ Created on Thu Aug 11 18:54:12 2016 @author: Administrato ...
- 特殊的Josn格式
static void Main(string[] args) { YtRequest<RequestHead, RequestBody> Ytrequ ...
- arpg网页游戏之地图(一)
[转]http://www.cnblogs.com/BlueWoods/p/4681572.html?from=timeline&isappinstalled=1 开发arpg网页游戏一项比较 ...
- 2.openssl rsa/pkey
分别是RSA密钥的处理工具和通用非对称密钥处理工具.它们用法基本一致,所以只介绍openssl rsa. [root@xuexi tmp]# man rsa NAME rsa - RSA key pr ...
- dockerfile学习与详解
1,什么是dockerfile? dockerfile是相当于docker使用的一个脚本,作用是便于实现自定义的镜像image,用语docker build [OPTIONS] PATH ,只需要指定 ...
- codevs1230 元素查找
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出n个正整数,然后有m个询问,每 ...
- enum 枚举的使用
在程序当中,我们经常定义一些常量来标识一些状态,类型等. 比如 定义订单的状态,可以定义为ORDER_STATUS_CANCEL = 1 表示订单状态为"订单已取消". 但是感觉定 ...
- vnc
Xvnc, Xvnc-core, vncagent, vncinitconfig, vnclicense, vnclicensehelper, vnclicensewiz, vncpasswd, vn ...
- CSS()方法设置元素样式
使用CSS()方法可以直接设置元素的样式,方法为:css(name,value) $(p).css("font-weight", "bold"); // ...