Learn With with by example:
[SQL]
View plaincopyprint?
- With
- -- Query the total salaries of departments and departments
- Dept_costs (
- Select D. department_name, sum (E. Salary) dept_total
- From orders ments D, employees e
- Where D. department_id = E. department_id
- Group
By D. department_name
- ),
- -- Calculate the average total salary of a department based on the result of the previous with query.
- Avg_costs (
- Select
AVG (dept_total) dept_avg
- From dept_costs
- )
- -- Compare two with queries and output query results
- Select *
- From dept_costs
- Where dept_total> (select dept_avg
From avg_costs)
- Order by department_name
With -- query the total salary of the Department and Department dept_costs as (select D. department_name, sum (E. salary) dept_total from departments D, employees e where D. department_id = E. department_id group by D. department_name), -- calculate the Department's average total salary avg_costs as (select AVG (dept_total) dept_avg from dept_costs) using the result of the previous with query) -- compare two with queries and output the query result select * From dept_costs where dept_total> (select dept_avg from avg_costs) order by department_name
Note:
① Subqueries can reuse the same or previous with query blocks and can be called Through select (The with clause can only be called by select)
② The query output of the with clause is stored in the user's temporary tablespace for one query and used everywhere
③ There are multiple query definitions before a SELECT statement of the same level. The first statement uses with, and the latter does not use with, and is separated by commas.
④ The last with query block and the following select call cannot be separated by commas (,). They are separated only by right brackets. The with clause query must be enclosed in brackets.
⑤ If the with clause is defined and is not used in the query, the ora-32035 error will be reported, as long as there is a reference to it, not necessarily in the Select call, it is also possible to query block references in later
⑥ The query defined by the with clause can be used in the with clause, but a with clause cannot be nested inside the with clause.
7. The result column of the with query has an alias. You must use an alias or *
Let's look at the with syntax.
(I) brackets in AS and select cannot be omitted.
(Ii) Select call at the same level. With can be defined only once. Multiple with subqueries are separated by commas, but there is no comma between the last with subquery and the actual query below.
Advantages of with clause
① The with clause may change the execution plan.
② The with subquery is executed only once, and the results are stored in the user's temporary tablespace, which can be referenced multiple times to increase performance.
③ SQL is highly readable.
Case:
I. general usage
[SQL]
View plaincopyprint?
- With
- -- Query the name of an employee in a sales department
- Saler_name (
- Select department_id
From orders ments where department_name = 'sales'
Order by department_id
- )
- Select last_name, first_name
- From employees e
- Where department_id in (select *
From saler_name)
With -- Query saler_name as (select department_id from departments where department_name = 'sales' order by department_id) of the employee in the sales department select last_name, first_name from employees e where department_id in (select * From saler_name)
Note: using the with clause, you can define a result set in a complex query and use it repeatedly in the query. If you do not use it, an error is returned. In addition, the with clause obtains a temporary table and must use select from (with query name)
II is referenced in most subqueries and visible at the same level
[SQL]
View plaincopyprint?
- Select last_name
- From (
- -- Query the name of an employee in a sales department
- Saler_name (
- Select department_id
From orders ments where department_name = 'sales'
Order by department_id
- )
- Select last_name, first_name
- From employees e
- Where department_id
In (select *
From saler_name)
- )
Select last_name from (with -- Query saler_name as (select department_id from departments where department_name = 'sales' order by department_id) Select last_name, first_name from employees e where department_id in (select * From saler_name ))
Iii. Reference in the collection
The two select calls of the set operation are treated as the same level, and two with definitions cannot appear.
[SQL]
View plaincopyprint?
- With
- -- Query the name of an employee in a sales department
- Saler_name (
- Select department_id
From orders ments where department_name = 'sales'
Order by department_id
- )
- Select last_name, first_name
- From employees e
- Where department_id in (select *
From saler_name)
- Union all
- Select last_name, to_char (null)
- From employees
Address: http://blog.csdn.net/linwaterbin/article/details/7990594