The WITH clause may be processed as an inline view or resolved as a temporary table. The advantage of the latter is that repeated references to the subquery may be more efficient as the data is easily retrieved from the temporary table, rather than being requeried by each reference.

WITH dept_count AS (

SELECT deptno, COUNT(*) AS dept_count

FROM   emp

GROUP BY deptno)

SELECT e.ename AS employee_name,

dc1.dept_count AS emp_dept_count,

m.ename AS manager_name,

dc2.dept_count AS mgr_dept_count

FROM   emp e,

dept_count dc1,

emp m,

dept_count dc2

WHERE  e.deptno = dc1.deptno

AND    e.mgr = m.empno

AND    m.deptno = dc2.deptno;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s