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练习的更多相关文章

  1. 010.简单查询、分组统计查询、多表连接查询(sql实例)

    -------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...

  2. mysql按年度、季度、月度、周、日统计查询的sql语句

    本文介绍一些mysql中用于查询的sql语句,包括按年度.季度.月度.周.日统计查询等,有需要的朋友,可以参考下. 一.年度查询 查询 本年度的数据   SELECT * FROM blog_arti ...

  3. thinkphp区间查询、统计查询、SQL直接查询

    区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是(and)并且的关系 //SELECT * FROM `tp_user` WHE ...

  4. 175. Combine Two Tables【LeetCode】-LEFT JON 和RIGHT JOIN,两张表关联查询-java -sql入门

    Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...

  5. 数据库联表统计查询 Group by & INNER JOIN

    原数据表 视频信息表  tab_video_info 播放记录表  tab_play_record 需求 统计播放量(已经开始播放)最多的前20个视频: SELECT a.video_id, SUM( ...

  6. ACTION 关联表之间查询语句 SQL语句写法

    /** EquUseRecord * @author cll * @return * @右边菜单中的使用记录操作 */ public String QueryAllEquUserecordAllInf ...

  7. hibernate实现多表联合查询

    转自:http://blog.sina.com.cn/s/blog_67b9ad8d01010by1.html 以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hiber ...

  8. hibernate 多表联合查询

    以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hibernate要实现多表联合查询的时候还楞了一下.最后看了下资料,才恍然大悟,hibernate实现多表联合查询跟SQ ...

  9. SQL调优--记一次表统计信息未及时更新导致查询超级慢

                某日同事丢给我一个看上去复杂的查询(实际就涉及两张表,套来套去)说只是换了日期条件,但一个查询5秒出数据,一个根本查不出来.现在整理下解决过程,及涉及的知识点. 若有不正之处, ...

随机推荐

  1. FPGA边缘检测

    沿检测技术在项目应用中, 非常低广泛. 如要有效捕获信号跳变沿, 边沿 检测技术的应用是必不可少的.大致如下: (1) 将时钟边沿使能转换为边沿检测使能,使时钟同步化. (2) 捕获信号的突变( UA ...

  2. Oracle 收缩表大小 Oracle Shrink Table --转载

    从10g开始,oracle开始提供Shrink的命令,假如我们的表空间中支持自动段空间管理 (ASSM),就可以使用这个特性缩小段,即降低HWM.这里需要强调一点,10g的这个新特性,仅对ASSM表空 ...

  3. SQL中使用WITH AS提高性能,使用公用表表达式(CTE)简化嵌套SQL

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...

  4. 解决Windows8下Cisco Systems VPN Client的Reason 442: Failed to Enable Virtual Adapter错误

    Windows8下使用Cisco Systems VPN Client创建的Cisco IPSec VPN无法连接,提示Reason 442: Failed to Enable Virtual Ada ...

  5. 正则验证:Pattern,Matcher

    public static void main(String[] args) { String regex="([a-z]{1})(\\d{2})"; String candida ...

  6. winform 控件(1)

    单词:controls(父类),所有的控件dataSource ,工具的数据源DisplayMember,属性,指定的值 <1>label--文本显示工具属性:1.text,是显示文字的n ...

  7. IE8下使用webuploader点击无反应的解决方法。

    在IE8开头添加如下代码. <meta http-equiv="X-UA-Compatible" content="IE=edge"> 即可解决,亲 ...

  8. VerbalExpressions ——另类正则表达式

    对于文本处理来说,正则表达式无疑是一个非常强大的工具.但是编写和阅读正则表达式往往就不是那么一件非常愉快的事情了.本文在这里介绍另一种另类的正则表达式——VerbalExpressions,它采用函数 ...

  9. 你还没成为Delphi QC的成员吗?(转红鱼儿)

    Delphi很早就建立了quality.embarcadero.com,简称为QC,质量控制中心,用来接收用户反馈的bug,新功能建议等,是开发者与delphi官方直接交流的平台.无论是否为正版用户, ...

  10. 模拟ATM机将输入的数据插入数据库

    ATM抽象类 public abstract class ATM { private double balance; private String idcard; private String pas ...