Updated April 5, 2023
Introduction to PL/SQL WITH
PL/SQL WITH clause is used for doing the subquery refactoring in complex queries that involve usage of the same result set of a particular query again and again. It is similar to the subqueries that are used in SQL but using the with a clause in the query reduces the complexity of debugging and execution of your query when it is very complex. The usage of with clause is also being done when we need a certain temporary table that will hold the data to be used by our query once or multiple times even without having any of the view or temporary table.
Syntax of PL/SQL WITH
WITH clause can be used which any of the sql statements of SELECT, INSERT, UPDATE or DELETE. The syntax of WITH clause is given below:
WITH new temp table (list of new columns) as
(statement for filling temp table),
statement(s) which use the temp table;
In the above syntax, the new temp table is the temporary table that can be referenced for retrieving the temporary result that is acquired by executing the statement for filling the temp table. The list of the columns that are specified is the names of the columns of the temporary table which should be the same as that of the number column values retrieved from the statement for filling the temp table. The keyword is used to alter mentioning the list of columns inside the brackets for the temporary table. The other statements using the result of the new temp table should be specified after giving a comma after filling the query.
Execution of Statement Containing WITH Clause
- The execution of the query statement containing the with clause gives the preference for calculating the result set of the clause which holds the with in it and retrieve the temporary result set in the new temp table.
- After this, the statement or statements which are specified other than the with clause are executed. These statements can make the use of the new temp table in it as many times as required. They will be executed after retrieving the result in the new temp table.
- The temporary table that is the new temp table is also called CTE which is a common table expression.
Usage of WITH Clause
There are many scenarios in which the usage of with clause may prove helpful in query execution and data management.
Here are some of the key cases where it can be used most probably to make efficient usage of executing and debugging the complex queries that are listed below:
- When it is not possible for creating a view in a database for using it in query statements.
- When we have to make use of the same result set multiple times inside the query statement.
- When we have to use the recursion while retrieving the result set.
Examples of PL/SQL WITH
Given below are the examples of PL/SQL WITH:
Example #1
Suppose we have a table named employee_details which contains the records as shown in the output of the below query statement.
Code:
SELECT * FROM [employee_details]
Output:
Now suppose, that we have to find out how much does salary the owner of the fruits and vegetable stores is to all the employees working in that particular store. In that case, the total salary for each store can be calculated as follows by using the with a clause for storing temporary results.
Code:
WITH temp_table AS (
SELECT
store_id as "store",
SUM(salary) salary
FROM
employee_details
WHERE
store_id IS NOT NULL
GROUP BY
store_id
)
SELECT
store, salary as "storewise salary"
FROM
temp_table
WHERE
store IN ("VEGETABLES","FRUITS");
Output:
The execution of the above query statement will be done firstly for the with statement query to store the details of all the stores and their total salaries I the table temp_table. After this, the execution of the other statement of retrieving the results for only those stores whose store id column will contain vegetables and fruits will be done. The output of the execution of the above query statement is as shown below showing the two stores and the total salary to be given by the owner for each of the stores respectively.
Example #2
Using the temporary result of WITH clause multiple times.
Let’s take one more example to understand the usage of with clause. There is one more table containing alternative contact mobile numbers of the employees named contact_details. The contents of this table can be retrieved by using the following query statement.
Code:
SELECT * FROM [contact_details]
Output:
Now, consider that we need to retrieve the records from the employee_details table that have a salary greater than the average salary. Along with that we also have to retrieve the mobile number present in the contact details table for that corresponding employee. In this case, we have to make use of the temporary table created by the with clause twice. One will be while calculating the average salary and the other one while retrieving the result set of it for getting the column details of those employees.
Code:
WITH temp_table AS(
SELECT
f_name, l_name, contact_number, mobile_number, salary
FROM
employee_details
INNER JOIN contact_details
ON employee_details.employee_id = contact_details.employee_id
ORDER BY
f_name
)
SELECT
f_name as "First Name",
l_name as "Last Name",
contact_number as "Contact Number 1",
mobile_number as "Contact Number 2",
salary
FROM
temp_table
WHERE
salary > (SELECT AVG(salary) FROM temp_table);
Output:
Conclusion
We can make use of the with a clause to get the result so that it can be used inside the particular query statement as a temporary table. The result set of the with clause can be referenced single or multiple times inside the same query statement. The with clause is most often used in complex query statements for efficient and easy execution and debugging of the query statement.
Recommended Articles
We hope that this EDUCBA information on “PL/SQL WITH” was beneficial to you. You can view EDUCBA’s recommended articles for more information.