ArticleslgStudy

science

Hierarchical and recursive queries in SQL

Hierarchical and recursive queries in SQL is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hierarchical and recursive queries in SQL rather than just read about it. In short: A hierarchical query is a type of SQL query that handles hierarchical model data. These are useful for working with databases of graph-structured data, such as river networks, file system trees, or threaded comments.

Key takeaways

  • Hierarchical and recursive queries in SQL belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hierarchical and recursive queries in SQL to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hierarchical and recursive queries in SQL from memory before moving on to harder problems.

Reference excerpt

A hierarchical query is a type of SQL query that handles hierarchical model data. These are useful for working with databases of graph-structured data, such as river networks, file system trees, or threaded comments. They are special cases of more general recursive fixpoint queries, which compute transitive closures. In standard SQL:1999 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 SELECT, INSERT, UPDATE, or DELETE 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:

where with_query's syntax is:

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 LEVEL below); if these are desired, they have to be created in the code. See MSDN documentation or IBM documentation for tutorial examples. The RECURSIVE 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 CREATE [RECURSIVE] VIEW. Using a CTE inside an INSERT INTO, 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:

CONNECT BY An alternative syntax is the non-standard CONNECT BY construct; 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. CONNECT BY 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:

For example,

The output from the above query would look like:

level | employee | empno | manager -------+-------------+-------+--------- 1 | KING | 7839 | 2 | JONES | 7566 | 7839 3 | SCOTT | 7788 | 7566 4 | ADAMS | 7876 | 7788 3 | FORD | 7902 | 7566 4 | SMITH | 7369 | 7902 2 | BLAKE | 7698 | 7839 3 | ALLEN | 7499 | 7698 3 | WARD | 7521 | 7698 3 | MARTIN | 7654 | 7698 3 | TURNER | 7844 | 7698 3 | JAMES | 7900 | 7698 2 | CLARK | 7782 | 7839 3 | MILLER | 7934 | 7782 (14 rows)

Pseudo-columns LEVEL CONNECT_BY_ISLEAF CONNECT_BY_ISCYCLE CONNECT_BY_ROOT

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:

Functions SYS_CONNECT_BY_PATH

See also Datalog also implements fixpoint queries Regular path queries are a specific kind of recursive query in graph databases Deductive databases Hierarchical model Recursive join Reachability Transitive closure Tree structure

References

Further reading C. J. Date (2011). SQL and Relational Theory: How to Write Accurate SQL Code (2nd ed.). O'Reilly Media. pp. 159–163. ISBN 978-1-4493-1640-2. Academic textbooks. Note that these cover only the SQL:1999 standard (and Datalog), but not the Oracle extension.

Abraham Silberschatz; Henry Korth; S. Sudarshan (2010). Database System Concepts (6th ed.). McGraw-Hill. pp. 187–192. ISBN 978-0-07-352332-3. Raghu Ramakrishnan; Johannes Gehrke (2003). Database management systems (3rd ed.). McGraw-Hill. ISBN 978-0-07-246563-1. Chapter 24. Hector Garcia-Molina; Jeffrey D. Ullman; Jennifer Widom (2009). Database systems: the complete book (2nd ed.). Pearson Prentice Hall. pp. 437–445. ISBN 978-0-13-187325-4.

External links "sql - Cycle detection with recursive subquery factoring - Stack Overflow". stackoverflow.com. Retrieved 2026-02-04. "SQL Server: are the recursive CTE's really set-based? at EXPLAIN EXTENDED". explainextended.com. Retrieved 2026-02-04. "Understanding the WITH Clause | Jonathan Gennick". Archived from the original on 2013-11-14. Retrieved 2026-02-04. "SQL: Recursion" (PDF). Archived from the original (PDF) on 2005-01-17. "BlackTDN :: MSSQL Usando Consulta CTE Recursiva para montagem de Tree". www.blacktdn.com.br. Retrieved 2026-02-04.

Worked examples

Example 1 — a first encounter with Hierarchical and recursive queries in SQL

Start with the simplest possible case. Write down what Hierarchical and recursive queries in SQL claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hierarchical and recursive queries in SQL before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hierarchical and recursive queries in SQL ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hierarchical and recursive queries in SQL

In research
Hierarchical and recursive queries in SQL appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hierarchical and recursive queries in SQL in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hierarchical and recursive queries in SQL is common in secondary-school and first-year university syllabi. It links to neighbouring topics Database management systems, Recursion, SQL, so understanding it makes those chapters shorter.
In everyday life
Look for Hierarchical and recursive queries in SQL outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Hierarchical and recursive queries in SQL” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Hierarchical and recursive queries in SQL in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hierarchical and recursive queries in SQL means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hierarchical and recursive queries in SQL out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hierarchical and recursive queries in SQL in simple terms?

A hierarchical query is a type of SQL query that handles hierarchical model data. These are useful for working with databases of graph-structured data, such as river networks, file system trees, or threaded comments.

Why does Hierarchical and recursive queries in SQL matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hierarchical and recursive queries in SQL?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hierarchical and recursive queries in SQL.

Tags

  • Database management systems
  • Recursion
  • SQL

Keep exploring