Hierarchical and recursive queries in SQL
A hierarchical query is a type of SQL query that handles hierarchical model data. They are special cases of more general recursive fixpoint queries, which compute transitive closures. In standard hierarchical queries are implemented by way of recursive "common table expressions" (CTEs). Unlike Oracle's earlier connect-by clause, recursive CTEs were designed with fixpoint semantics from the beginning. Recursive CTEs from the standard were relatively close to the existing implementation in IBM DB2 version 2. Recursive CTEs are also supported by Microsoft SQL Server (since SQL Server 2008 R2), Firebird 2.1, PostgreSQL 8.4 +, SQLite 3.8.3+, IBM Informix version 11.50+, CUBRID, MariaDB 10.2+ and MySQL 8.0.1+. Tableau has documentation describing how CTEs can be used. TIBCO Spotfire does not support CTEs, while Oracle 11g Release 2's implementation lacks fixpoint semantics. Without common table expressions or connected-by clauses it is possible to achieve hierarchical queries with user-defined recursive functions. Common table expression. A common table expression, or CTE, (in SQL) is a temporary named result set, derived from a simple query and defined within the execution scope of a codice_1, codice_2, codice_3, or codice_4 statement. CTEs  can be thought of as alternatives to derived tables (subquery), views, and inline user-defined functions. Common table expressions are supported by Teradata (starting with version 14), IBM Db2, Informix (starting with version 14.1), Firebird (starting with version 2.1), Microsoft SQL Server (starting with version 2005), Oracle (with recursion since 11g release 2), PostgreSQL (since 8.4), MariaDB (since 10.2), MySQL (since 8.0), SQLite (since 3.8.3), HyperSQL, Informix (since 14.10), Google BigQuery, Sybase (starting with version 9), Vertica, H2 (experimental), and many others. Oracle calls CTEs "subquery factoring". The syntax for a CTE (which may or may not be recursive ) is as follows: WITH [RECURSIVE] with_query [, ...] SELECT ... where codice_5‘s syntax is: query_name [ (column_name [...]) ] AS (SELECT ...) Recursive CTEs can be used to traverse relations (as graphs or trees) although the syntax is much more involved because there are no automatic pseudo-columns created (like codice_6 below); if these are desired, they have to be created in the code. See MSDN documentation or IBM documentation for tutorial examples. The codice_7 keyword is not usually needed after WITH in systems other than PostgreSQL. In SQL:1999 a recursive (CTE) query may appear anywhere a query is allowed. It's possible, for example, to name  the result using codice_8 [codice_7] codice_10. Using a CTE inside an codice_11, one can populate a table with data generated from a recursive query; random data generation is possible using this technique without using any procedural statements. Some Databases, like PostgreSQL, support a shorter CREATE RECURSIVE VIEW format which is internally translated into WITH RECURSIVE coding. An example of a recursive query computing the factorial of numbers from 0 to 9 is the following: WITH RECURSIVE temp (n, fact) AS (SELECT 0, 1 -- Initial Subquery SELECT * FROM temp; CONNECT BY. An alternative syntax is the non-standard codice_12 construct; it  was introduced by Oracle in the 1980s. Prior to Oracle 10g, the construct was only useful for traversing acyclic graphs because it returned an error on detecting any cycles; in version 10g Oracle introduced the NOCYCLE feature (and keyword), making the traversal work in the presence of cycles as well. codice_12 is supported by Snowflake, EnterpriseDB, Oracle database, CUBRID, IBM Informix and IBM Db2 although only if it is enabled as a compatibility mode. The syntax is as follows: SELECT select_list FROM table_expression [ WHERE ... ] [ START WITH start_expression ] [ ORDER SIBLINGS BY column1 [ ASC | DESC ] [, column2 [ ASC | DESC  ] ] ... ] [ GROUP BY ... ] [ HAVING ... ] SELECT LEVEL, LPAD (' ', 2 * (LEVEL - 1)) || ename "employee", empno, mgr "manager" FROM emp START WITH mgr IS NULL CONNECT BY PRIOR empno = mgr; The output from the above query would look like: Unary operators. The following example returns the last name of each employee in department 10, each manager above that employee in the hierarchy, the number of levels between manager and employee, and the path between the two: SELECT ename "Employee", CONNECT_BY_ROOT ename "Manager", LEVEL-1 "Pathlen", SYS_CONNECT_BY_PATH(ename, '/') "Path" FROM emp WHERE LEVEL > 1 and deptno = 10 CONNECT BY PRIOR empno = mgr  ORDER BY "Employee", "Manager", "Pathlen", "Path"; Further reading. Academic textbooks. Note that these cover only the SQL:1999 standard (and Datalog), but not the Oracle extension.