热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

AdvancedOracleSQL:SubqueryInlineView

AdvancedOracleSQL:SubqueryInlineViewOracleTipsbyLaurentSchneiderLaurentSchneiderisconsideredoneofthetopOracleSQLexperts,andheistheauthorofthebookAdvancedSQLProgrammingbyRampantTechPress.Thefollowingisa

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<2500;

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"<2500)

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<2500;
WITH
DEPT10
AS
(
SELECT
EMPNO,
ENAME,
SAL
FROM
EMP
WHERE
DEPTNO=10
)
SELECT
ENAME
FROM
DEPT10
WHERE
SAL<2500;

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.

推荐阅读
  • 本文详细介绍了如何使用libpq库与PostgreSQL后端建立连接。通过探讨PQconnectdb()函数的工作原理及其在实际应用中的使用方法,帮助读者理解并掌握建立高效、稳定的数据库连接的关键步骤。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 使用C#开发SQL Server存储过程的指南
    本文介绍如何利用C#在SQL Server中创建存储过程,涵盖背景、步骤和应用场景,旨在帮助开发者更好地理解和应用这一技术。 ... [详细]
  • 本文探讨了适用于Spring Boot应用程序的Web版SQL管理工具,这些工具不仅支持H2数据库,还能够处理MySQL和Oracle等主流数据库的表结构修改。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 本文详细介绍了如何在 Linux 平台上安装和配置 PostgreSQL 数据库。通过访问官方资源并遵循特定的操作步骤,用户可以在不同发行版(如 Ubuntu 和 Red Hat)上顺利完成 PostgreSQL 的安装。 ... [详细]
  • 如何在PostgreSQL中查看数据表
    本文将指导您使用pgAdmin工具连接到PostgreSQL数据库,并展示如何浏览和查找其中的数据表。通过简单的步骤,您可以轻松访问所需的表结构和数据。 ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 在使用SQL Server进行动态SQL查询时,如果遇到LIKE语句无法正确返回预期结果的情况,通常是因为参数传递方式不当。本文将详细探讨这一问题,并提供解决方案及相关的技术背景。 ... [详细]
  • 本文介绍如何通过创建替代插入触发器,使对视图的插入操作能够正确更新相关的基本表。涉及的表包括:飞机(Aircraft)、员工(Employee)和认证(Certification)。 ... [详细]
author-avatar
西西为考研要拼命啦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有