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

边栏推荐
- Official clarification statement of Jihu company
- Codeforces 12D ball tree array simulation 3 sorting elements
- MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
- PIP install beatifulsoup4 installation failed
- Multiplexing of Oracle control files
- MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
- 华为快游戏调用登录接口失败,返回错误码 -1
- K210学习笔记(四) K210同时运行多个模型
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- NET中小型企业项目开发框架系列(一个)
猜你喜欢

K210学习笔记(四) K210同时运行多个模型

EBS Oracle 11g cloning steps (single node)

华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017

Create a virtual machine on VMware (system not installed)

Daily question brushing record (XIV)

MMAP

华为快游戏调用登录接口失败,返回错误码 -1

The real situation of programmers

Reptile practice

Shell script, awk uses if, for process control
随机推荐
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
Huawei cloud modelarts text classification - takeout comments
EBS Oracle 11g 克隆步骤(单节点)
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
Sentinel production environment practice (I)
HDU 4391 paint the wall segment tree (water
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Cold violence -- another perspective of objective function setting
Recovery technology with checkpoints
How to develop and introduce applet plug-ins
NET中小型企业项目开发框架系列(一个)
装饰器学习01
Microservice link risk analysis
The American Championship is about to start. Are you ready?
Shell script, awk uses if, for process control
Analyse des risques liés aux liaisons de microservices
大约SQL现场“这包括”与“包括在”字符串的写法
Multiplexing of Oracle control files
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Poj3414广泛搜索