PostgreSQL - Recursive Query Using CTEs
Last Updated :
08 Aug, 2024
Recursive queries are a powerful feature in PostgreSQL that allow you to perform iterative operations within a database. While strictly speaking, this process is iteration, the SQL standards committee chose the term RECURSIVE. This article will provide an in-depth understanding of PostgreSQL recursive queries, their syntax, examples, and best practices to ensure your queries are efficient and effective.
General Structure of a Recursive Query
The general structure of the PostgreSQLĀ recursive query contains,
- Non-recursive SELECT statement
- UNION or UNION ALL
- Recursive SELECT statement
Syntax
WITH RECURSIVE name_cte AS (
SELECT statement /* non-recursive statement */
UNION [ALL]
SELECT statement /*recursive statement referencing the above select statement */
)
SELECT * FROM name_cte;
How Does a PostgreSQL Recursive Query Work?
- Evaluate Non-Recursive Statements: The initial SELECT statement is evaluated, creating a temporary table.
- Evaluate Recursive Terms: The recursive SELECT statement is evaluated and its results are added to the temporary table.
- Repeat: Step 2 is repeated until no more rows can be added to the temporary table.
Union vs. Union All
- UNION: Eliminates duplicate rows.
- UNION ALL: Allows duplicates.
PostgreSQL Recursive Query Using CTEs Example
Let us take a look at some of the examples of Recursive Query Using CTEs in PostgreSQL to better understand the concept.
Example 1: Generating a Sequence of Numbers
This is the basic example of PostgreSQL recursive query which prints the first 10 natural numbers.
Query:
WITH RECURSIVE tens AS (
SELECT 1 as n
UNION ALL
SELECT n+1 FROM tens
)
SELECT n FROM tens limit 10;

Explanation: This query generates the first 10 natural numbers.
Example 2: Calculating Factorials
PostgreSQL recursive query to find factorial of a natural number.
Query:
WITH RECURSIVE fact (n, factorial)
AS (
SELECT 1 as n, 5 as factorial
union all
SELECT n+1, factorial*n FROM fact WHERE n < 5
)
SELECT * FROM fact;
Explanation: This query calculates the factorial of numbers up to 5, showing each step of the calculation.

Example 3: Generating Fibonacci Series
PostgreSQL recursive query to print Fibonacci series.
Query:
WITH RECURSIVE fibb
AS (
SELECT 1::bigint as n, 0::bigint as a, 1::bigint as b
UNION ALL
SELECT n+1, b as a, (a+b) as b FROM fibb
)
SELECT b FROM fibb limit 10;

Explanation: This query generates the first 10 numbers in the Fibonacci series.
Example 4: Finding Organizational Hierarchy
With the help of PostgreSQL recursive query, we can find the organizational hierarchy:
PostgreSQL
-- Creating the employees table and inserting data
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
full_name VARCHAR(100),
manager_id INTEGER );
INSERT INTO employees (
employee_id,
full_name,
manager_id
)
VALUES
(1, 'Abhi', NULL),
(2, 'Bhargav', 1),
(3, 'Chay', 1),
(4, 'Dravid', 1),
(5, 'Erin', 1),
(6, 'Ford', 2),
(7, 'Gagan', 2),
(8, 'Harry', 3),
(9, 'Isaac', 3),
(10, 'Jack', 4),
(11, 'Kiran', 5);

Abhi is the boss, he will be on the first level. Bhargav, Chay, Dravid, Erin are in the next level and the rest of them will be the last level.
Query:
WITH RECURSIVE subordinates AS (
SELECT employee_id, manager_id, full_name, 0 as level
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.manager_id, e.full_name, level+1
FROM employees e
INNER JOIN subordinates s ON s.employee_id = e.manager_id
)
SELECT * FROM subordinates;
The output will be:

Explanation: This query retrieves the hierarchical structure of employees, starting from the top-level manager (Abhi) and including all subordinates at each level.
Best Practices for Using PostgreSQL Recursive Queries
- If duplicates are not an issue, use UNION ALL instead of UNION to improve performance.
- Use a WHERE clause to limit the depth of recursion, preventing potential infinite loops.
- Ensure relevant columns are indexed to speed up join operations in recursive queries.
- Use descriptive names for common table expressions (CTEs) to enhance readability.
- Regularly monitor query performance, especially for large datasets.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read
Use Case Diagram - Unified Modeling Language (UML)
A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read