当前位置:网站首页>With as subquery in Oracle
With as subquery in Oracle
2022-06-30 15:39:00 【Milk coffee 13】
One 、WITH AS brief introduction
Oracle stay Oracle 9i In the second version of the database, we introduced the SQL; With Clause SQL Allows you to give a subquery block a name ( Also known as subquery refactoring ), Can be in the Lord SQL References in multiple locations in the query ; The subquery works like Oracle's global temporary tables , Used to improve complexity SQL Query speed .
① This subquery is used to define temporary relationships , So that the output of this temporary relationship can be used , And used by the query associated with the clause .
② Queries related to subqueries can also be written using nested subqueries , But doing so will add more complex reads / debugging SQL Inquire about .
③ This subquery does not support all database systems ;
④ The names assigned to subqueries are like online views or tables ;
⑤Oracle stay Oracle 9i edition 2 In the database, the..., with clauses, is introduced SQL.
⑥with as The temporary table is cleared after the query is completed .
⑦ Added sql The readability of , If multiple subqueries are constructed , The structure will be clearer .
Two 、WITH AS The syntax of the subquery
The syntax of the subquery :
WITH Query name 1 AS ( Subquery ) , Query name 2 AS ( Subquery ), Query name 3 AS ( Subquery ) The clause part of the subquery definition 
3、 ... and 、 Use scenarios
3.1、 Find all employees whose salaries exceed the average salary of all employees
① Employee table information

② Find all employees whose salaries exceed the average salary of all employees WITH AS Subquery statement
WITH tmpTable ( avgValue ) AS ( SELECT AVG( "Salary" ) FROM "EmployeeInfo" )
SELECT
*
FROM
"EmployeeInfo",
tmpTable
WHERE
"EmployeeInfo"."Salary" > tmpTable.avgValueexplain : In this query , Define a temporary relation table using a subquery tmpTable, This temporary table has only 1 Average of attributes avgValue【 namely AVG("Salary") Content 】; Then, we use the query statement to compare the salary and average value of each employee in the basic table (9600) Compare , Only output employee information that is greater than the average salary .
Be careful : When executing a query with clauses , First evaluate the query mentioned in the clause , And store the output of this assessment in a temporary relationship . After that , The main query that will use the associated clause is finally executed , The query will use the resulting temporary relationship .
③ Finding results

3.2、 Get the average salary of employees 、 The highest salary 、 Total salary
① Employee information form

② Get the average salary of employees 、 Maximum salary and total salary
WITH
tmpAvgAgeTable1 AS (SELECT ROUND(AVG("Salary"), 2) avgSalary FROM "EmployeeInfo"),
tmpMaxAgeTable2 AS (SELECT MAX("Salary") maxSalary FROM "EmployeeInfo" ),
tmpSumAgeTable3 AS (SELECT SUM("Salary") sumSalary FROM "EmployeeInfo")
SELECT * FROM tmpAvgAgeTable1 t1,tmpMaxAgeTable2 t2 ,tmpSumAgeTable3 t3③ Query results

Four 、 Reference material
SQL (oracle.com)
https://docs.oracle.com/database/121/CNCPT/sqllangu.htm
边栏推荐
- Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants
- FoxPro and I
- Xiao Sha's pain (thinking problem)
- 4.1 print function
- Infrastructure is code. What are you talking about?
- H - Arctic network (minimum spanning tree)
- Npumcm selection question 3 and acmc2020a
- Bye civil engineering, hello CS, can you change the certificate to the Blue Bridge Cup
- 深入理解.Net中的线程同步之构造模式(二)内核模式1.内核模式构造物Event事件
- linux下修改mysql密码: ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘
猜你喜欢

Management joint examination - mathematical formula

Talk about why I started technical writing

topic: Privacy, Deception and Device Abuse

Kubernetes: a comprehensive analysis of container choreography

linux下修改mysql密码: ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘

Webrtc: industrial application based on Internet of things

It's so brain - burning that no wonder programmers lose their hair

What would you choose between architecture optimization and business iteration?

Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)

Super comprehensive redis distributed high availability solution: sentry mechanism
随机推荐
1018 public bike Management (30 points)
Technology sharing | how to quickly realize audio and video online calls
1015 reversible primes (20 points)
Specific steps for installing mysql8.0 on Windows system
【子矩阵数量统计】CF1181C Flag子矩阵数量统计
linux下新建Mysql数据库并导入sql文件
G - navigation nightare
1062 talent and virtue (25 points)
Imitating freecodecamp to build a programming ability test platform
Rte2021 review HDR technology product practice and exploration
剑指 Offer II 080. 含有 k 个元素的组合 回溯
数据治理市场:亿信华辰朝左,华傲数据向右
The sound network has fully opened the real-time transmission network sd-rtn, which has been free of network wide accidents for seven years - this is FPA!
Anyrtc implements application scenarios based on webrtc
Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
(Niuke) BFS
Basic literacy - four common software architectures
【Leetcode】链表排序(逐步提高时空复杂度)
B. Moamen and k-subarrays (codeforce+ binary search)
Fundamentals of C language -- similarities and differences between arrays and pointers