Unit2_Rercursive
Unit2_Rercursive
WITH Clause
SQL> SELECT EName , Empno, Mgr FROM SQL> select e1.mgr,e2.ename mgrName
EMP; ,e1.empno ,e1.ename Ename
2 from emp e1, emp e2 where
e1.mgr=e2.empno;
SQL> SELECT e.MGR, e.EMPNO MGR EMPNO *This output is the rows indicated
---------- ---------- by arrow in previous slide
2 FROM EMP e
3 WHERE e.MGR = 7566; 7566 7902
7788 7876
7902 7369
7566 7788
7902 7788
7369 7876
Recursive Query-WITH Clause
• Recursive query is applied to query hierarchical data using recursive subquery factoring.
Recursion is implemented in standard SQL-99 using Common Table Expressions (CTEs).
• A CTE can be thought of as a named temporary table within a SQL statement that is retained
for the duration of that statement.
• A CTE is defined at the beginning of a query using the WITH clause.
• There can be multiple CTE and each CTE must have Unique name.
In general, in recursion there has to be seed and recursion stopping criteria.