This article brings you relevant knowledge about Oracle advanced queries, including group queries, multi-table queries and subqueries. I hope it will be helpful to everyone.
Advanced query is widely used in the development process of database, from group query, multi-table query and sub-query Introduce Oracle's advanced queries in three aspects.
Grouping query is to group according to certain rules. After grouping, the data will be aggregated. Aggregation functions need to be used, but grouping is not necessary to use aggregate functions. The keyword for grouping is group by.
Commonly used aggregation functions include: maximum max(), minimum min(), average avg(), sum sum(), and statistical number count()
Use of count function Null values will be automatically ignored when naming the column
nvl function can prevent count from automatically ignoring null values. Its function is to return 0 when comm is empty, because 0 is non-empty. So it will go into the statistical total.
All columns in the select list that are not included in the aggregate function should be included in the group by in clause.
Find the average salary of each department, display the department number and the average salary of the department.
select deptno,avg(sal) from emp group by deptno order by deptno
Statistics on the total salary of employees by department and different positions
select detpno,job,sum(sal) from emp group by deptno,job order by deptno
Use of having clause
The difference between where and having
Note: From the perspective of SQL optimization, try to use where, because where greatly reduces the number of grouped records, thus Improve efficiency.
Find the departments whose average salary is greater than 2000
select deptno,avg(sal) from emp where(avg(sal)) > 2000 group by deptno
The aggregate function cannot be used in the where clause, so an error is reported, change it to having The xxx clause is sufficient.
select deptno,avg(sal) from emp group by deptno having(avg(sal)) > 2000
in the group query to find the average salary of each department, display the department number, and the average salary of the department, arranged in ascending order of salary.
select deptno,avg(sal) from emp group by deptno order by avg(sal)
You can also sort by the alias of the column
select deptno,avg(sal) avgsal from emp group by deptno order by avgsal
You can also sort by the serial number of the column. The average salary is the second column
select deptno,avg(sal) from emp group by deptno order by 2
Just add desc in descending order
select deptno,avg(sal) from emp group by deptno order by 2 desc
Find the maximum value of the average salary of the department
select max(avg(sal)) from emp group by deptno
Mainly used in the group by statement report function
Each department, install different positions, find the total salary, department summary, summary.
You can use the rollup function
select deptno,job,sum(sal) from emp group by rollup(deptno,job)
and then set the display format. Break on deptno means that the same department number only Display one, skip 1 means there is a blank line between different department numbers.
Add title, page number, etc.
ttitle col 15 'My Report' col 35 sql.pno
Set the title, empty 15 columns to display my report, and then empty 35 columns to display the page number
col deptno heading department number
col job heading position
col sum(sal) heading Total salary
Set column headers for the above 3 lines
break on deptno skip 1
Set the display format, the same department number only Display one, with a blank line between different department numbers
Save these settings to a sql file (note that it must be changed to ANSI encoding, otherwise garbled characters will appear and the settings will be invalid), and then read and executed through the get command. Execute the query statement again and get the following report. If multiple pages appear, in order to display them beautifully, you can set one page to display more lines, for example, set each page to display 100 lines: set pagesize 100
The above examples all query data from a single table. Let’s start with querying data from multiple tables.
In order to avoid the Cartesian set, you can add valid connection conditions to where. Under actual circumstances, the use of the Cartesian complete set should be avoided.
实例:查询员工信息,要求显示:员工号,姓名,月薪,部门名称
需要查询员工表和部门表,通过部门号进行等值连接查询,where xxx=xxx
select e.empno,e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno
示例:查询员工信息,要求显示:员工号,姓名,月薪,薪水级别
需要查询员工表和薪水等级表,通过薪水等级上下限进行不等值连接查询。where xxx between xxx and xxx,注意:小值在between前面,大值在between后面
select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal
示例:按部门统计员工人数,要求显示:部门号,部门名称,人数
需要查询部门表和员工表
以下是通过等值连接的方式查询,虽然总人数没有问题,但是少了一个部门,因为一个部门没有员工。
select d.deptno 部门号,d.dname 部门名称,count(e.empno) 人数 from emp e,dept d where e.deptno=d.deptno group by d.deptno,d.dname
外连接一般通过join来实现,一张图看懂SQL的各种join用法。
使用join语句重新实现示例功能
select d.deptno 部门号,d.dname 部门名称,count(e.empno) 人数 from dept d left join emp e on d.deptno=e.deptno group by d.deptno,d.dname
示例:查询员工姓名和员工的老板姓名
核心:通过别名,将同一张表视为多张表
select e.ename 员工姓名,b.ename 老板姓名 from emp e, emp b where e.mgr=b.empno
这种方式会产生笛卡尔集,不适合大表的查询,可以使用层次查询来解决。connect by xxx start with xxx
level是层次查询提供的伪列,需要显示使用才会查询这个伪列。
select level,empno,ename,sal,mgr from emp connect by prior empno=mgr start with mgr is null order by 1
必须要有小括号,书写风格要清晰如下图所示:
示例:查询比FORD工资高的员工
select * from emp where sal > (select sal from emp where ename='FORD')
select,from,where,having
select位置的子查询只能是单行子查询,也就是只能返回一条结果
select empno,ename,sal,(select job from emp where empno='7839') job from emp
示例:查找部门平均工资大于30号部门最大工资的部门号及其平均工资
select deptno,avg(sal) from emp group by deptno having avg(sal) > (select max(sal) from emp where deptno=30)
from位置的子查询
查询结果也可以当成表
select * from (select empno,ename,sal from emp)
增加1列年薪,使用sal*12得到年薪
select * from (select empno,ename,sal,sal*12 annsal from emp)
示例:查询部门名称是SALES的员工信息
使用子查询的方式:
select * from emp where deptno=(select deptno from dept where dname='SALES')
使用多表查询的方式:
select e.* from emp e, dept d where e.deptno=d.deptno and d.dname='SALES'
一般不在子查询中,使用排序;但在Top-N分析问题中,必须对子查询排序
示例:找到员工表中工资最高的前三名,如下格式:
rownum,行号,oracle自动为表分配的伪列。
select rownum,empno,ename,sal from (select * from emp order by sal desc) where rownum<h3><strong>子查询执行顺序</strong></h3><p>一般先执行子查询,再执行主查询;单相关子查询例外。</p><p>相关子查询示例:找到员工表中薪水大于本部门平均薪水的员工</p><pre class="brush:php;toolbar:false">select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal > (select avg(sal) from emp where deptno=e.deptno)
单行子查询返回一个结果,只能使用单行操作符;
多行子查询返回多个结果,只能使用多行操作符。
操作符 | 含义 |
---|---|
= | 等于 |
> | 大于 |
>= | 大于等于 |
小于 | |
小于等于 | |
不等于 |
操作符 | 含义 |
---|---|
in | 等于列表中的任何一个 |
any | 和子查询返回的任意一个值比较 |
all | 和子查询返回的左右值比较 |
查询员工信息,要求:
职位与7566员工一样,薪水大于7782员工的薪水
select * from emp where job=(select job from emp where empno=7566) and sal >(select sal from emp where empno=7782)
查询最低工资大于20号部门最低工资的部门号和部门的最低工资
select deptno,min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp where deptno=20)
查询部门名称是SALES和ACCOUNTING的员工信息
使用多行子查询的方式:
select * from emp where deptno in (select deptno from dept where dname='SALES' or dname='ACCOUNTING')
使用多表查询的方式:
select e.* from emp e, dept d where e.deptno=d.deptno and (d.dname='SALES' or d.dname='ACCOUNTING')
查询不是老板的员工
注意:当子查询中包含null值时,不要使用not in。
a not in (10,20,null)
a != 10 and a != 20 and a != null, a != null 永远不成立,所以整个表达式永远返回false。
可以在子查询中把null值过滤掉再使用not in。
select * from emp where empno not in (select mgr from emp where mgr is not null)
推荐教程:《Oracle教程》
The above is the detailed content of Let's talk about Oracle advanced queries (detailed examples). For more information, please follow other related articles on the PHP Chinese website!