Oracle学习笔记——第二天

oracle学习笔记——第二天

一.分组语句 group by

在这里插入图片描述
— group by last_name ( 姓一样的同学被分到一组)
— group by last_name,first_name( 只有姓和名完全一样的同学被分到一组)

— 请查询各部门最高工资
思路:
1.确定数据来源表: from employees 107行
2.数据筛选 : 没有 107行
3.确定分组条件: group by department_id
4.统计数据: select department_id, max(salary)
合并:

 Select department_id,max(salary) from employees group by department_id ;

— 请统计1997年各部门的入职员工人数
思路:
1.from employees 107行
2.where to_char(hire_date,‘yyyy’)=1997 29行
3.group by department_id
4.select department_id, count(*)
合并:

    Select department_id,count(*) 
    from employees 
    where to_char(hire_date,’yyyy’)=1997 
    group by department_id;

— 请统计1997年各个月入职的员工人数

1.from employees
2.where to_char(hire_date,’yyyy’)=1997
3.group by to_char(hire_date,’mm’)
4.select to_char(hire_date,’mm’),count(*) 

在这里插入图片描述

二.分组后数据筛选 :having

在这里插入图片描述
— 请查询各部门人数,只显示人数大于2的信息

       Select department_id,count(*) From employees 
       group by department_id having count(*)>2

【重点】
在这里插入图片描述
— 请查询30,50部门的最高工资

   1.Select department_id,max(salary) from employees where department_id in(30,50)
     Group by department_id; 
   2.Select department_id,max(salary) from employees group by department_id
     Having department_id in(30,50)

三.select六条子句总结【重点】

在这里插入图片描述

四.伪列 【重点】

1.数据库表里不存在的,通过select * 无法查询到的列 — rownum rowid

Select * , rownum , rowid from employees; – error
解决

   Select  employees.* , rowid , rownum from employees; 
   Select  e.*,rowid,rownum from employees e ;

2.Rowid : 是一条记录在数据库里的唯一标识,是对记录所在空间的物理地址运算得到

3.Rownum:数据库服务器会为每一个出现在查询结果里的,满足要求的记录进行编号,

从1开始编号
— 请打印员工表的前五行数据

   Select * from employees where rownum<=5;
  --- 请打印员工表的第6到第10行数据
   Select * from employees where rownum between 6 and 10;      error
  **注意: rownum必须从1开始使用(<   <=    =1  >=1   between 1 and … )**

五.子查询

1.概念 :

– 请查询公司里工资最高的员工信息
– 1. 查询最高工资是多少 result

  select max(salary) from employees ;  

– 2. 查询员工信息,工资等于result

  select * from employees where salary = result ;

– 合并

    select * from employees 
    where salary = ( select max(salary) from employees );
   概念:在一个查询命令执行过程中,嵌入另外一个查询语句(子查询)
   作用:提供主查询需要的数据

2.子查询结果为单行单列 — 一个值 【重点】

  -- 请查询公司里工资高于平均工资的员工信息
-- 1. select avg(salary) from employees;  result
-- 2. select * from employees where salary > result ;
   select * from employees
   where salary > ( select avg(salary) from employees) ;

3.子查询的结果为多行一列 ---- 多个值 【了解】

– 请查询与‘King’在同一部门工作的员工信息
– 1. 查询King所在的部门编号

  select department_id from employees where last_name='King';  

80和90
– 2. 查询在 80和90部门工作的员工

  select * from employees where department_id in (select department_id from employees where    last_name='King');

4.子查询结果是多行多列 — 一张表(虚拟表)【重点】

作用:可以针对虚拟表做二次查询
–请查询员工表里工资最高的五名员工信息
– 1. 对员工表数据做排序 按照工资降序排列

  select * from employees order by salary desc;  --- result 虚拟表

– 2. 对结果数据筛选 where rownum<=5

  select * from (select * from employees order by salary desc) where rownum<=5

数据分页:对满足要求的数据分段显示【重点】

1)显示的数据不需要排序

– 打印表里的第六到第10名员工信息
– 1.显示表里的前10行数据

  select e.*,rownum rn from employees e where rownum<=10

– 2. 利用虚拟表里的普通字段rownum进行数据查询

    select * from 
    (select e.*,rownum rn from employees e where rownum<=10)
    where rn between 6 and 10;

总结 – 查询第m到第n行数据
第一步:先查询前n行数据,同时生成rn字段(rownum的别名)、
第二步:根据rn字段获取需要的数据

2)显示的数据需要排序

开发步骤—查询第m到第n行数据
第一步:先对表里的数据按要求排序 tab1
第二步:针对tab1先查询前n行数据,同时生成rn字段(rownum的别名)、
第三步:根据rn字段获取需要的数据
– 打印工资最高的第六到第10名员工信息
– 1. 先排序

 	select * from employees order by salary desc   -- t1

– 2. 查询前n行,生成rn

	select t1.*,rownum rn from t1 where rownum<=10  -- t2

– 3. 根据rn获取需要的数据

    select * from t2 where rn between 6 and 10;

– 合并

   		select * 
   		from (  select t1.*,rownum rn 
           		from (select * from employees order by salary desc) t1 
           		where rownum<=10 ) 
  		where rn between 6 and 10;

5.关联子查询【 了解 】

— 打印各部门工资最高的员工信息
在这里插入图片描述

   Select * from employees e1 where salary = (select max(salary) from employees e2
   Where e2.department_id= e1.department_id ) ;

难点:
1)Where是逐行判断
2)表的别名可以标志查询语句
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值