当前位置:网站首页>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 .
边栏推荐
- EL与JSTL注意事项汇总
- A long's perception
- Serializability of concurrent scheduling
- Huawei fast game failed to call the login interface, and returned error code -1
- Codeforces 12D Ball 树形阵列模拟3排序元素
- SecureCRT使用提示
- 科技云报道:算力网络,还需跨越几道坎?
- Cold violence -- another perspective of objective function setting
- Pointer parameter passing vs reference parameter passing vs value parameter passing
- Defect detection - Halcon surface scratch detection
猜你喜欢
PyGame practical project: write Snake games with 300 lines of code
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
Shell script, awk condition judgment and logic comparison &||
MMAP learning
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Database recovery strategy
Meituan dynamic thread pool practice ideas, open source
Two stage locking protocol for concurrency control
Implementation technology of recovery
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
随机推荐
Recovery technology with checkpoints
如何组织一场实战攻防演练
Overview of concurrency control
K210学习笔记(四) K210同时运行多个模型
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
"Chris Richardson microservices series" uses API gateway to build microservices
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
MMAP学习
AD637 usage notes
Summary of El and JSTL precautions
DataGrid directly edits and saves "design defects"
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
NET中小型企业项目开发框架系列(一个)
Index optimization of performance tuning methodology
笔记本电脑蓝牙怎么用来连接耳机
极狐公司官方澄清声明
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Codeforces 12D ball tree array simulation 3 sorting elements