An inline view or a query uses subquery in it.
Subquery execution done more efficiently rather by using the query repetadly.
On case by case the performance varies with WITH clause in a sql query.
Below example shows how to reduce repetition of query and simplify complex SQL statements.
SELECT e.ename AS employee_name,
dc.d_count AS emp_dept_count
FROM emp e,
(SELECT deptid, COUNT(*) AS d_count
FROM emp
GROUP BY deptid) dc
WHERE e.deptid = dc.deptid;
Query will be like as below by using a WITH clause.
WITH d_count AS (
SELECT deptid, COUNT(*) AS dept_count
FROM emp
GROUP BY deptid)
SELECT e.ename AS employee_name,
dc.dept_count AS emp_dept_count
FROM emp e,
d_count dc
WHERE e.deptid = dc.deptid;
This way we can make multiple references to the subquery more efficient.
Sometime,WITH clause can be used for a subquery to use as a global temporary table or the query to be use as inline query.
Subquery execution done more efficiently rather by using the query repetadly.
On case by case the performance varies with WITH clause in a sql query.
Below example shows how to reduce repetition of query and simplify complex SQL statements.
SELECT e.ename AS employee_name,
dc.d_count AS emp_dept_count
FROM emp e,
(SELECT deptid, COUNT(*) AS d_count
FROM emp
GROUP BY deptid) dc
WHERE e.deptid = dc.deptid;
Query will be like as below by using a WITH clause.
WITH d_count AS (
SELECT deptid, COUNT(*) AS dept_count
FROM emp
GROUP BY deptid)
SELECT e.ename AS employee_name,
dc.dept_count AS emp_dept_count
FROM emp e,
d_count dc
WHERE e.deptid = dc.deptid;
This way we can make multiple references to the subquery more efficient.
Sometime,WITH clause can be used for a subquery to use as a global temporary table or the query to be use as inline query.