— 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(*)
Select department_id,count(*) From employees
group by department_id having count(*)>2
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 * , rownum , rowid from employees; – error
解决
Select employees.* , rowid , rownum from employees;
Select e.*,rowid,rownum from employees e ;
从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. 查询最高工资是多少 result
select max(salary) from employees ;
– 2. 查询员工信息,工资等于result
select * from employees where salary = result ;
– 合并
select * from employees
where salary = ( select max(salary) from employees );
概念:在一个查询命令执行过程中,嵌入另外一个查询语句(子查询)
作用:提供主查询需要的数据
-- 请查询公司里工资高于平均工资的员工信息
-- 1. select avg(salary) from employees; result
-- 2. select * from employees where salary > result ;
select * from employees
where salary > ( select avg(salary) from employees) ;
– 请查询与‘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');
作用:可以针对虚拟表做二次查询
–请查询员工表里工资最高的五名员工信息
– 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
– 打印表里的第六到第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字段获取需要的数据
开发步骤—查询第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;
Select * from employees e1 where salary = (select max(salary) from employees e2
Where e2.department_id= e1.department_id ) ;