In my previous article i have given the different SQL tutorials with real life examples.In this article i will give you the Oracle With Clause information with its examples. You might have seen that so many reporting tool queries starts with Oracle With Clause.The With Clause is mainly used to improve the performance of the specific query.The Oracle 9i provides the new functionality of With Clause.The with clause is just like temporary tables in oracle.Mainly the with clause is used to execute subqueries in fast way.It is called as sub-query factoring.
In this section i would like to explain the syntax as well as examples of With clause in oracle.Before checking the syntax and examples of With clause in oracle let us first check some important bullet points of With Clause :
These are some important points of With Clause in oracle. In following section i would like to explain about the syntax as well as multiple examples of with clause.
Syntax :
With SQL_Query_Name As
(
SQL query;
)
Select * from New_SQL_Query_name;
Execution of With Clause :
In this section i would like to explain about the step by step execution of With clause.
Step 1 : The SQL Query within the with clause is executed at first step.
Step 2 : The output of the SQL query is stored in to temporary relation of with clause.
Step 3 : The Main query is executed with temporary relation produced at last stage.
With Clause with Aggregation :
In this section i would like to give you example with its syntax for With clause with aggregation.There are so many times the With clause is used in oracle .
Syntax :
WITH
sub-query_name
AS
(The aggregation SQL statement)
SELECT
(query name sub-query_name);
Real life example 1 :
Let us take the example of Student table.If user wants to find out the Students from Student table whose marks is more than Average of all marks.
With Student_Temp(Average_Marks)
As
(Select Avg(Marks) From Student),
Select Roll_No,Name,Marks From Student , Student_Temp
Where Student.Marks > Student_Temp.Average_Marks);
The above example will give you the Students whose marks are greater than average marks with using With Clause with Aggregation.
Real Life Example 2 :
There are some situations where user needs to calculate Salary of the Employee with Total number of the Employees and user needs to show it department-wise then following query is useful.
With Department_Count As
(
Select Dept_No,Count(Emp_No) As ‘No_Of_Employees’
From Employee
Group by Dept_No
)
Select Employee_No,
Salary/No_Of_Employees
From Employee E,
Department_Count C
Where E.Dept_no = C.Dept_No;
Points to Remember :
In this section, I would like to give some important points to remember while using With Clause in Oracle.
Point 1 :
The With clause in Oracle can be beneficial for complex SQL queries. For Simple SQL queries dont use with clause to make it complicated.
Point 2 :
The with clause fragments the complex subqueries in to queries.It is used for simple debugging of the query.
Point 3 :
The With clause in oracle is nothing but the drop -in replacement for normal subqueries.
Point 4 :
When User find the sub-query and needs to improve the performance ; Try to use With clause and convert that sub-query in to With clause query to improve the performance.
These are above some most important points to remember while using With clause in Oracle. I hope this article will be useful for users. If you like this article or if you have any concerns with the same kindly comment in to comments section.
In my previous article I have given details about application support engineer day to day…
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…