Last Updated : 23 Jul, 2025
In SQL, a recursive join is a powerful technique used to handle hierarchical data relationships, such as managing employee-manager relationships, family trees, or any data with a self-referential structure. This type of join enables us to combine data from the same table repeatedly, accumulating records until no further changes are made to the result set.
In this article, we will explore recursive joins in SQL, understand the concept of recursive common table expressions (CTEs), and work through detailed examples to illustrate how to use recursive joins effectively.
What is a Recursive Join in SQL?Recursive joins are implemented using recursive common table expressions (CTEs). CTEs are temporary result sets that can be referred to within the execution scope of a SELECT, INSERT, UPDATE, or DELETE statement. In a recursive CTE, a query is repeatedly executed to gather related data, making it possible to handle hierarchical relationships like parent-child data.
Syntax:
WITH RECURSIVE cte_name AS (Example of Recursive Join in SQL
-- Anchor Query: Select the root or starting point
SELECT columns
FROM table
WHERE condition
UNION ALL
-- Recursive Query: Join the CTE with the table to fetch related data
SELECT t.columns
FROM table t
INNER JOIN cte_name cte ON t.column = cte.column
)
Let’s walk through an example where we create an employee-manager hierarchy using a recursive join in SQL. Assume we have a table of employees where each employee has a manager_id pointing to their manager’s employee_id. The goal is to retrieve a list of employees along with their managers, all the way up the chain.
employee_id employee_name manager_id age 1 Ankit NULL 32 2 Ayush 1 31 3 Piyush 1 42 4 Ramesh 2 31 5 Rohan 3 29 6 Harry 3 28 7 Rohit 4 32 8 Gogi 4 32 9 Tapu 5 33 10 Sonu 5 40Now, we will use a recursive join to get a list of all employees and their managers, starting with Ankit (employee with employee_id = 1).
Query:
WITH RECURSIVE employee_hierarchy AS (
-- Anchor query: Start with Ankit (employee_id = 1)
SELECT employee_id, employee_name, manager_id, age
FROM employees
WHERE employee_id = 1
UNION ALL
-- Recursive query: Join the employees table with itself to get the employees reporting to each manager
SELECT e.employee_id, e.employee_name, e.manager_id, e.age
FROM employees e
INNER JOIN employee_hierarchy eh ON e.manager_id = eh.employee_id
)
SELECT * FROM employee_hierarchy;
Output:
Explanation:
Recursive joins in SQL, implemented through recursive CTEs, are a vital tool for querying hierarchical data efficiently. Whether it's navigating organizational structures, analyzing product categories, or working with parent-child relationships, recursive joins simplify the process of building and querying hierarchies. By using the WITH RECURSIVE clause and combining it with an INNER JOIN, SQL provides a powerful way to traverse and retrieve nested data.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4