Use of with AS statements in Oracle

Source: Internet
Author: User

I. Introduction to WITH AS:

With AS is actually a subquery that can be used to enhance the readability of SQL, because the query is executed only once and the results are stored in the user temp table space.

Can be referenced more than once, enhancing performance.


Two. With AS usage:

With temp as (     select * from XXX) SELECT * from temp;

two. With as combat:

The department that inquires the total salary of the department is greater than the average total salary of all departments:

Department table s_dept, employee table s_emp;

--Step1: Query The total salary of department name and department with Dept_costs as (select A.name,sum (b.salary) dept_total from   s_dept a,s_emp b   wher E a.id=b.dept_id   GROUP by A.name),--Step2: Calculates the average total salary of the Department avg_costs as (select sum (dept_total)/count (*) with the results of the previous with query. Dept_avg from Dept_costs)--step3: Compare and output query results from two with queries select Name,dept_total from   dept_costswheredept_total > (  select Dept_avg from avg_costs) order by name;


Use of with AS statements in Oracle

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.