当前位置:网站首页>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 .

边栏推荐
- Three components of openpyxl
- HDU 4391 Paint The Wall 段树(水
- Overview of database recovery
- AD637使用筆記
- Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
- A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
- Oracle HugePages没有被使用导致服务器很卡的解决方法
- Efficiency difference between row first and column first traversal of mat data types in opencv
- Poj3414广泛搜索
猜你喜欢

多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

AD637使用筆記

How can Huawei online match improve the success rate of player matching

Daily question brushing record (XIV)

资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌

ICMP 介绍

Ad637 notes d'utilisation

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

Database tuning solution
随机推荐
深信服X计划-网络协议基础 DNS
poj 3237 Tree(樹鏈拆分)
DataGrid directly edits and saves "design defects"
Analyse des risques liés aux liaisons de microservices
Ad637 notes d'utilisation
HDU 4391 Paint The Wall 段树(水
Robot operation mechanism
华为快游戏调用登录接口失败,返回错误码 -1
Meituan dynamic thread pool practice ideas, open source
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
阿龙的感悟
Cold violence -- another perspective of objective function setting
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
如何组织一场实战攻防演练
Codeforces 12D ball tree array simulation 3 sorting elements
K210 learning notes (IV) k210 runs multiple models at the same time
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
The real situation of programmers
QML reported an error expected token ";", expected a qualified name ID
MMAP学习