oracle学习笔记(十一) 高级查询
2023-02-18 16:37:02 时间
高级查询
分组查询
select * from student
[where ]
[having ] --二次限定
[order by] --asc升序 desc降序 默认升序
查看EMPLOYEE表中,员工在同一部门做相同工作且总工资大于5000,平均工资小于1500的薪水情况和人数?
select deptno,job,count(*) from employee
group by deptno,job
having avg(sal)<1500 and sum(sal) >5000
order by deptno;
分组查询注意事项:
如果在查询结果中的某些字段上没有使用分组函数,那么这些字段就必须出现在group by子句中。这是一个分组查询的强制性规则。
--查询每个部门的人数以及部门的总工资
--两个列都是使用了分组函数,所以group by 语句中可以省略不写
--但deptno没有使用分组函数,所以要group by语句中要出现deptno
select deptno,count(empno),sum(sal) from employee
group by deptno
非等值连接
简单地来说,连接条件就是某些值满足在某个范围之内
--salgrade表中三个属性,losal-hisal是工资范围,grade则是该范围对应的等级
select ename,salgrade from employee,salgrade
where sal between losal and hisal;
内连接inner join(等值连接)
多用于表之间有关联关系,如主外键关系的表
select * from employee e
join department d --这里相当于inner join,inner可以省略
on e.deptno = d.deptno;
--等值连接
select * frjom employee e,department d
where e.deptno = d.deptno;
左外连接left outer join
多用于表之间有关联关系,如主外键关系的表,不过以左边数据为标准,未匹配的以空行出现
一般outer可以省略不写
select * from employee e
left join depaterment d on e.deptno = d.deptno;
右外连接
与左外连接相反,以右边数据为标准,未匹配的以空行出现,好像不常用
子查询
单行子查询可以出现在以下位置:
- select语句
- from语句
- join子句中
- HAVING子句中
多行子查询:
- ANY(值列表): 与子查询结果列表中的任何一个值比较。
- ALL(值列表):与子查询结果列表中的所有值比较。
- IN(值列表): 等于列表中的任何一个值。
select *
from employee
where sal<any(select distinct sal from employee where job='SALESMAN')
相关文章
- 3xx HTTP状态码的终极指南
- React报错之React.Children.only expected to receive single React element child
- React报错之Element type is invalid
- React报错之Too many re-renders
- React报错之Function components cannot have string refs
- 如何发布一个 TypeScript 编写的 npm 包
- 如何使用zx编写shell脚本
- 13个构建RESTful API的最佳实践
- 什么是REST API
- React报错之Rendered more hooks than during the previous render
- 如何使用CSS伪类选择器
- React报错之Property 'value' does not exist on type EventTarget
- React报错之Parameter 'event' implicitly has an 'any' type
- React报错之Parameter 'props' implicitly has an 'any' type
- React报错之Property 'value' does not exist on type 'HTMLElement'
- React报错之You provided a `checked` prop to a form field
- React报错之Invalid hook call
- React报错之React hook 'useState' cannot be called in a class component
- React报错之React Hook 'useEffect' is called in function
- React报错之React hook 'useState' is called conditionally