ORACLE with AS usage

Source: Internet
Author: User

Syntax:

With Tempname as (select ...)
Select ...

For example: Now you want to get 11-14 from 1-19. The general SQL is as follows:

SELECT *
from (
            --simulate a 20-row data SELECT level as
             lv from
               DUAL CONNECT by Level
         <
) TT
 wher E tt.lv > tt.lv < 15

SQL with AS is:

With TT as (
                --simulate a 20-row data Select level as
                 LV from
                 DUAL CONNECT by Level
                <
             ) 
Select LV From TT
WHERE LV > LV < 15

The WITH query statement does not start with a select, but begins with the "with" keyword
You can assume that a temporary table TT was constructed beforehand before the query was actually made, and then you can use it more than once to do further analysis and processing

advantages of the WITH clause method

Increased SQL readability, if multiple subqueries are constructed, the structure will be clearer, and more importantly: "One analysis, multiple use", which is why the performance of the place, reached the "less read" goal. The

 first method table that uses a subquery is scanned two times, and a table is scanned only once using the WITH clause method. This can greatly improve the efficiency of data analysis and query.

 In addition, observe the WITH clause method execution plan, where "sys_temp_xxxx" is the intermediate statistic result temporary table constructed during the operation.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.