当前位置:网站首页>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
边栏推荐
- Matlab finds prime numbers within 100
- 容器常用命令
- 终于看懂科学了!200张图领略人类智慧的巅峰
- 数据治理市场:亿信华辰朝左,华傲数据向右
- 数数据可视化实战案例(timeline轮播图,streamlit 控件年份 metabase可视化使用教程)2.0
- E - highways (minimum spanning tree)
- 1027 colors in Mars (20 points)
- About pickle module - 6 points that beginners must know
- 1031 Hello world for u (20 points)
- Operator%
猜你喜欢

《你的灯亮着吗》开始解决问题前,得先知道“真问题”是什么

Model system: Sword (1)

How to do a good job in high concurrency system design? I have summarized three points

BYD is more and more like Huawei?

开源 STM32 USB-CAN项目

Advanced C language - pointer 3 - knowledge points sorting

About pickle module - 6 points that beginners must know

Four solutions to cross domain problems

How should we understand the variability of architecture design?

Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants
随机推荐
M - smooth engineering continuation (minimum spanning tree)
K - rochambau (joint search, enumeration)
Infrastructure is code. What are you talking about?
1148 werewolf - Simple Version (20 points)
At the beginning of the 2022 new year, I will send you hundreds of dry articles
Oracle中的With As 子查询
Policy Center > Deceptive Behavior
Oculus quest2 | unity configures the oculus quest2 development environment and packages an application for real machine testing
A little idea about big experiment data
O - ACM contest and blackout (minimum spanning tree, Kruskal)
How to get palindrome number in MATLAB (using fliplr function)
K - or unblocked project (minimum spanning tree)
Google Play 索引表
[matlab] 3D drawing summary
Fundamentals of C language -- similarities and differences between arrays and pointers
1035 password (20 points)
Rte2021 review HDR technology product practice and exploration
Policy Center > Misrepresentation
1058 a+b in Hogwarts (20 points)
What would you choose between architecture optimization and business iteration?