首頁 資料庫 mysql教程 Advanced Oracle SQL: Subquery Inline View

Advanced Oracle SQL: Subquery Inline View

Jun 07, 2016 pm 03:46 PM
i oracle sql

Advanced Oracle SQL: Subquery Inline View Oracle Tips by Laurent Schneider Laurent Schneider is considered one of the top Oracle SQLexperts, and he is the author of the book Advanced SQL Programming by Rampant TechPress. The following is a

Advanced Oracle SQL: Subquery Inline View

Oracle Tips by Laurent Schneider

 

Laurent Schneider is considered one of the top Oracle SQLexperts, and he is the author of the book "Advanced SQL Programming"by Rampant TechPress.  The following is an excerpt from the book.

In the FROM clause below, a subquery acts as a table:

SELECT
   ENAME
FROM
(
   SELECT
      EMPNO,
      ENAME,
      SAL
   FROM
      EMP
   WHERE
      DEPTNO=10
)
WHERE
   SAL
ENAME
----------
CLARK
MILLER

--------------------------------------------------------------------------
| Id  | Operation         | Name |Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    13 |    3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |     1|    13 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

   1 - filter("DEPTNO"=10 AND "SAL"

The subquery returns all employees of department 10, and the main queryreturns only those with a salary less than 2500.

The subquery could be saved as a view, providing the necessary privilegesare granted. In fact, a subquery in the FROM clause is called an inline viewand might look like the following where the view is created before theselection.  The selection starts with the WITH statement:

CREATE VIEW
   DEPT10
AS
SELECT
   EMPNO,
   ENAME,
   SAL
FROM
   EMP
WHERE
   DEPTNO=10
/
SELECT
   ENAME
FROM
   DEPT10
WHERE
   SAL WITH
   DEPT10
AS
(
   SELECT
      EMPNO,
      ENAME,
      SAL
   FROM
      EMP
   WHERE
      DEPTNO=10
)
SELECT
   ENAME
FROM
   DEPT10
WHERE
   SAL

Subquery factoring was introduced in Oracle 9i. Instead of using asubquery, the two conditions, salary less than 2500 and department equal 10,could be combined by an AND logical operator.

Nested subquery

Subqueries can be used in logical statements like =ALL, >SOME, =, > followed by SOME, ANY or ALL, the left operand iscompared with multiple values of the subquery. IN checks if the left value isin the subquery. NOT IN checks if the left value is not in the subquery. WithIN and NOT IN, it is possible to have an expression list on the left side. Thenumber of columns of the subquery must match the number of expressions in theleft expression list. EXISTS has no left operand and checks if the subqueryreturns at least one row. The number of columns is irrelevant, so star (*) isjust fine. NOT EXISTS is true when the subquery returns no rows.

The three queries that follow create the same result and the same plan:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   DEPTNO!=ALL
   (
      SELECT
         EMP.DEPTNO
      FROM
         EMP
      WHERE
         EMP.DEPTNO IS NOT NULL
   );
 
    DEPTNO
----------
        40

------------------------------------------------------------------------------
| Id  | Operation          |Name    | Rows  | Bytes | Cost (%CPU)|Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |         |     3|    18 |     5  (20)| 00:00:01 |
|*  1 |  HASH JOIN ANTI   |         |     3|    18 |     5  (20)| 00:00:01 |
|   2 |   INDEX FULL SCAN  | PK_DEPT |    4 |    12 |    1   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| EMP    |    14 |    42 |    3   (0)| 00:00:01 |
------------------------------------------------------------------------------

   1 - access("DEPTNO"="EMP"."DEPTNO")
   3 - filter("EMP"."DEPTNO" IS NOT NULL)

Or:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   DEPTNO NOT IN
   (
      SELECT
         EMP.DEPTNO
      FROM
         EMP
      WHERE
         EMP.DEPTNO IS NOT NULL
   );
 
    DEPTNO
----------
        40

------------------------------------------------------------------------------
| Id  | Operation          |Name    | Rows  | Bytes | Cost (%CPU)|Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |         |     3 |   18 |     5  (20)| 00:00:01 |
|*  1 |  HASH JOIN ANTI   |         |     3|    18 |     5  (20)| 00:00:01 |
|   2 |   INDEX FULL SCAN  | PK_DEPT|     4 |    12 |    1   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| EMP    |    14 |    42 |    3   (0)| 00:00:01 |
------------------------------------------------------------------------------
   1 - access("DEPTNO"="EMP"."DEPTNO")
   3 - filter("EMP"."DEPTNO" IS NOT NULL)

Or:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   NOT EXISTS
   (
      SELECT
         *
      FROM
         EMP
      WHERE
         EMP.DEPTNO=DEPT.DEPTNO
   );
 

    DEPTNO
----------
        40

------------------------------------------------------------------------------
| Id  | Operation          |Name    | Rows  | Bytes | Cost (%CPU)|Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |         |     3|    18 |     5  (20)| 00:00:01 |
|*  1 |  HASH JOIN ANTI   |         |     3|    18 |     5  (20)| 00:00:01 |
|   2 |   INDEX FULL SCAN  | PK_DEPT|     4 |    12 |     1  (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| EMP    |    14 |    42 |    3   (0)| 00:00:01 |
------------------------------------------------------------------------------

   1 -access("EMP"."DEPTNO"="DEPT"."DEPTNO")

From the department table, the department that is different from alldepartments in EMP is returned.

A subquery in the WHERE clause is called a nested subquery.  The joinbetween the two tables is an antijoin.

It is important to note the NOT NULL condition in NOT IN and !=ALL. If onedepartment is null in EMP, it should not exclude department 40:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   DEPTNO=SOME
   (
      SELECT
         EMP.DEPTNO
      FROM
         EMP
   );

    DEPTNO
----------
        10
        20
        30

-------------------------------------------------------------------------------
| Id  |Operation           |Name    | Rows  | Bytes | Cost (%CPU)|Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |        |     3 |    18 |    4  (25)| 00:00:01 |
|   1 |  NESTED LOOPS      |         |     3|    18 |     4  (25)| 00:00:01 |
|   2 |   SORT UNIQUE      |         |    14|    42 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL|EMP     |    14 |    42|     3   (0)| 00:00:01 |
|*  4 |   INDEX UNIQUE SCAN | PK_DEPT |    1 |     3 |     0   (0)|00:00:01 |
-------------------------------------------------------------------------------

   4 - access("DEPTNO"="EMP"."DEPTNO")

Or:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   DEPTNO IN
   (
      SELECT
         EMP.DEPTNO
      FROM
         EMP
   ); 

    DEPTNO
----------
        10
        20
        30

-------------------------------------------------------------------------------
| Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)|Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |         |     3|    18 |     4  (25)| 00:00:01 |
|   1 |  NESTED LOOPS      |         |     3|    18 |     4  (25)| 00:00:01 |
|   2 |   SORT UNIQUE      |         |    14|    42 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL|EMP     |    14 |    42|     3   (0)| 00:00:01 |
|*  4 |   INDEX UNIQUE SCAN | PK_DEPT |    1 |     3 |     0   (0)|00:00:01 |
-------------------------------------------------------------------------------

   4 -access("DEPTNO"="EMP"."DEPTNO")

Or:

SELECT
   DEPTNO
FROM
   DEPT
WHERE
   EXISTS
   (
      SELECT
         *
      FROM
         EMP
      WHERE
         EMP.DEPTNO=DEPT.DEPTNO
   );

    DEPTNO
----------
        10
        20
        30

-------------------------------------------------------------------------------
| Id  | Operation          | Name    |Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |         |     3|    18 |     4  (25)| 00:00:01 |
|   1 |  NESTED LOOPS      |         |     3|    18 |     4  (25)| 00:00:01 |
|   2 |   SORT UNIQUE      |         |    14|    42 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL|EMP     |    14 |    42|     3   (0)| 00:00:01 |
|*  4 |   INDEX UNIQUE SCAN | PK_DEPT |    1 |     3 |     0   (0)|00:00:01 |
-------------------------------------------------------------------------------

   4 -access("EMP"."DEPTNO"="DEPT"."DEPTNO")

This type of join is called a semijoin.

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
oracle游標關閉怎麼解決 oracle游標關閉怎麼解決 Apr 11, 2025 pm 10:18 PM

解決 Oracle 游標關閉問題的方法包括:使用 CLOSE 語句顯式關閉游標。在 FOR UPDATE 子句中聲明游標,使其在作用域結束後自動關閉。在 USING 子句中聲明游標,使其在關聯的 PL/SQL 變量關閉時自動關閉。使用異常處理確保在任何異常情況下關閉游標。使用連接池自動關閉游標。禁用自動提交,延遲游標關閉。

oracle怎麼循環創建游標 oracle怎麼循環創建游標 Apr 12, 2025 am 06:18 AM

Oracle 中,FOR LOOP 循環可動態創建游標, 步驟為:1. 定義游標類型;2. 創建循環;3. 動態創建游標;4. 執行游標;5. 關閉游標。示例:可循環創建游標,顯示前 10 名員工姓名和工資。

oracle日誌寫滿怎麼辦 oracle日誌寫滿怎麼辦 Apr 12, 2025 am 06:09 AM

Oracle 日誌文件寫滿時,可採用以下解決方案:1)清理舊日誌文件;2)增加日誌文件大小;3)增加日誌文件組;4)設置自動日誌管理;5)重新初始化數據庫。在實施任何解決方案前,建議備份數據庫以防數據丟失。

oracle視圖怎麼導出 oracle視圖怎麼導出 Apr 12, 2025 am 06:15 AM

可以通過 EXP 實用程序導出 Oracle 視圖:登錄 Oracle 數據庫。啟動 EXP 實用程序,指定視圖名稱和導出目錄。輸入導出參數,包括目標模式、文件格式和表空間。開始導出。使用 impdp 實用程序驗證導出。

HDFS配置CentOS需要哪些步驟 HDFS配置CentOS需要哪些步驟 Apr 14, 2025 pm 06:42 PM

在CentOS系統上搭建Hadoop分佈式文件系統(HDFS)需要多個步驟,本文提供一個簡要的配置指南。一、前期準備安裝JDK:在所有節點上安裝JavaDevelopmentKit(JDK),版本需與Hadoop兼容。可從Oracle官網下載安裝包。環境變量配置:編輯/etc/profile文件,設置Java和Hadoop的環境變量,使系統能夠找到JDK和Hadoop的安裝路徑。二、安全配置:SSH免密登錄生成SSH密鑰:在每個節點上使用ssh-keygen命令

甲骨文在商業世界中的作用 甲骨文在商業世界中的作用 Apr 23, 2025 am 12:01 AM

Oracle不僅是數據庫公司,還是雲計算和ERP系統的領導者。 1.Oracle提供從數據庫到雲服務和ERP系統的全面解決方案。 2.OracleCloud挑戰AWS和Azure,提供IaaS、PaaS和SaaS服務。 3.Oracle的ERP系統如E-BusinessSuite和FusionApplications幫助企業優化運營。

oracle數據庫怎麼停止 oracle數據庫怎麼停止 Apr 12, 2025 am 06:12 AM

要停止 Oracle 數據庫,請執行以下步驟:1. 連接到數據庫;2. 優雅關機數據庫(shutdown immediate);3. 完全關機數據庫(shutdown abort)。

centos上weblogic的數據庫連接如何配置 centos上weblogic的數據庫連接如何配置 Apr 14, 2025 pm 02:06 PM

在CentOS系統上配置WebLogic數據庫連接,需要完成以下步驟:JDK安裝與環境配置:確保服務器已安裝與WebLogic版本兼容的JDK(例如,WebLogic14.1.1通常需要JDK8)。正確設置JAVA_HOME、CLASSPATH和PATH環境變量。 WebLogic安裝與解壓:從Oracle官方網站下載適用於CentOS系統的WebLogic安裝包,並將其解壓到指定目錄。 WebLogic用戶與目錄創建:創建一個專用的WebLogic用戶賬戶,並設置安全密碼

See all articles