当前位置:网站首页>Oracle advanced query
Oracle advanced query
2022-07-05 22:02:00 【The sea of waves】
Oracle Advanced query
Group query
The concept of grouping function
Grouping functions act on a set of data , And return a value for a set of data .
Use of grouping functions
The grouping function automatically ignores null values , Can pass nvl() Go for special treatment .

AVG and SUM
// Calculate the average salary and total salary of employees
select avg(sal), sum(sal) from emp;
MIN and MAX
// Find out the maximum and minimum wage of employees
select MAX(sal),MIN(sal) from emp;
COUNT
// Find out the total number of employees
select count(*) from emp;
WM_CONCAT( Transfer line column )
select deptno,wm_concat(ename) from emp group by deptno;

Use Group by Clause data grouping
// Calculate the average salary of each department
select deptno,avg(sal) from emp group by deptno;
partition by Partition function
select deptno,row_number() over(partition by ename order deptno) from emp group by deptno;
Partition function partition by Usage of
Use Having Sentence filter grouping result set

Can't be in Where Use group function in clause , Can be in Having Use group function in clause
**where and having If it can be used universally , Use as much as possible where **,having First group and then filter ,where First filter and then group .
GROUP BY Sentence enhancement ( Make reports )
effect

Realization
select deptno,job,sum(sal) from emp group by deptno,job;
+
select deptno,sum(sal) from emp group by deptno;
+
select sum(sal) from emp;
The above three statements are equal to the following one
====
select deptno,job,sum(sal) from emp group by rollup(deptno,job);
Multiple table joins
Cartesian collection ( Multi-table query )

Equivalent connection
Query employee information , Ask to show : Employee number , full name , a monthly salary , Department name
select e.empno,e.ename,e.sal,d.dname from emp e ,dept d where e.deptno = d.deptno;
Unequal value connection
Query employee information , Ask to show : Employee number , full name , a monthly salary , The salary level of employees
select e.empno,e.ename,e.sal,s.grade from emp,salgrade s where e.sal between s.losal and s.hisal;
External connection

Count the number of employees by department , Ask to show : Department number , Department name , The number of
// Right connection (dept Main table ) , If you can understand it, you can select d.deptno,d.dname,count(e.empno) The number of from emp e, dept dwhere e.deptno(+)=d.deptnogroup by d.deptno,d,dname;// I suggest you write this select d.deptno,d.dname,count(e.empno) The number of from emp e left join dept don e.deptno=d.deptnogroup by d.deptno,d,dname;// The left outer join (emp Main table )select d.deptno,d.dname,count(e.empno) The number of from emp e, dept dwhere e.deptno=d.deptno(+)group by d.deptno,d,dname;select d.deptno,d.dname,count(e.empno) The number of from emp e right join dept don e.deptno=d.deptnogroup by d.deptno,d,dname;

Self join
Query the employee's name and the employee's boss's name
select e.ename Employee name ,b.ename The name of the boss from emp e,emp bwhere e.mgr = b.empno

- Not suitable for large table operation

select level,empno,ename,sal,mgrfrom empconnect by piror empno = mgrstart with mgr is nullorder by 1;
- Single table query , There is no obvious result of self connection
Subquery

You can use the location of the subquery where,select ,from,having
select Location use subquery

stay having Location use subquery

stay from Location use subquery

Cannot use the location of subquery group by
Top-N Query questions
// Query the information of the top three employees with the highest wages select rownum, empno,enmame,sal from (select * from emp order by sal desc) where rownum <= 3
Two problems need to be paid attention to in line number
- Line numbers are always generated in the default order
- The line number can only be used <.,<=; Out of commission >,>=
Sub query null Value problem ( Many lines )

Case study
Paging query
// Query the first 5 To 8 The record of the line
select r,empno,ename,sal
(select rownum r,empno,ename,sal
from (select rownum ,empno,ename,sal from emp order by sal desc) e1
where rownum <=8) e2
where r >=5
Find the employee whose salary in the employee list is higher than the average salary of the Department
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)
select e.empno,e.ename,e.sal,d.avgsal
from emp e, (select deptno,avg(sal) avgsal from emp group by deptno) d
where e.deptno = d.deptno and e.sal > d.avgsal;
There is something wrong , Welcome to the discussion .
Last , Welcome to pay attention to my wechat , What do you like , Collection , Forwarding is my greatest encouragement .

边栏推荐
- 854. String BFS with similarity K
- NET中小型企业项目开发框架系列(一个)
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
- 阿龙的感悟
- Meituan dynamic thread pool practice ideas, open source
- Business learning of mall order module
- DBeaver同时执行多条insert into报错处理
- 笔记本电脑蓝牙怎么用来连接耳机
- oracle 控制文件的多路复用
猜你喜欢

Lightweight dynamic monitorable thread pool based on configuration center - dynamictp

从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析

微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)

Shell script, awk condition judgment and logic comparison &||

Shell script, awk uses if, for process control

Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need

The American Championship is about to start. Are you ready?

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

Recovery technology with checkpoints

MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
随机推荐
How to use tensorflow2 for cat and dog classification and recognition
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Business learning of mall commodity module
Multiplexing of Oracle control files
Codeforces 12D Ball 树形阵列模拟3排序元素
Analysis and test of ModbusRTU communication protocol
Robot framework setting variables
Storage optimization of performance tuning methodology
让开发效率提升的跨端方案
Bitbucket installation configuration
K210学习笔记(四) K210同时运行多个模型
Comment développer un plug - in d'applet
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
Livelocks and deadlocks of concurrency control
[Yugong series] go teaching course in July 2022 004 go code Notes
Index optimization of performance tuning methodology
Oracle HugePages没有被使用导致服务器很卡的解决方法
EBS Oracle 11g 克隆步骤(单节点)