Explorer
SQL

Find all employees and their managers using a recursive CTE.

Problem Statement

<p>Find all <code>employees</code> and their managers using a recursive CTE.</p>

Examples

Input: employees table: +----+-------+------------+ | id | name | manager_id | +----+-------+------------+ | 1 | Boss | NULL | | 2 | Alice | 1 | | 3 | Bob | 2 | +----+-------+------------+

Output: +----+-------+------------+-------+ | id | name | manager_id | level | +----+-------+------------+-------+ | 1 | Boss | null | 1 | | 2 | Alice | 1 | 2 | | 3 | Bob | 2 | 3 | +----+-------+------------+-------+

Explanation: The query retrieves the requested records satisfying all problem requirements.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Determine which tables contain the required columns and relate them using INNER JOIN on the corresponding foreign/primary keys. šŸ’” Hint 2: Write out the ON condition matching key fields (e.g. ON a.key = b.key) and apply any filtering in the WHERE clause. šŸ’” Hint 3: Select only the requested columns in the SELECT clause with clear aliases if needed: SELECT ... FROM ... INNER JOIN ... ON ...;

Editorial & Approach

Problem Overview & Intuition

To solve "Recursive CTE (Hierarchical Data)", we query the relational database engine using declarative SQL. The goal is to find all employees and their managers using a recursive cte. By formulating an optimal execution plan with appropriate projection and filtering, the database engine executes the query with minimal overhead.

Step-by-Step Approach

  1. Analyze Schema: Identify the target tables, necessary foreign keys, and expected output columns.
  2. Construct Filtering & Logic: Apply table joins to isolate the requested data.
  3. Format & Order: Ensure columns match the expected project schema in order.

Optimal Implementation (SQL)

WITH RECURSIVE emp_hierarchy AS (SELECT id, name, manager_id, 1 AS level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, e.manager_id, h.level + 1 FROM employees e JOIN emp_hierarchy h ON e.manager_id = h.id) SELECT * FROM emp_hierarchy;

Complexity Analysis

Time Complexity O(N * M) worst-case, O(N + M) with hash/merge join on indexed keys.
Space Complexity O(N) for join buffer and result set.

Key Considerations & Edge Cases

  • Empty Tables: The query executes safely returning zero rows without syntax error.
  • NULL Values: Columns containing NULL values are properly handled by standard ANSI SQL semantics.
  • Case Sensitivity: String comparisons and keywords adhere to PostgreSQL/standard SQL rules.

Recursive CTE (Hierarchical Data)

Hard

Find all employees and their managers using a recursive CTE.

Example Scenarios
1Example 1
Input:
employees table
idnamemanager_id
1BossNULL
2Alice1
3Bob2
Output:
idnamemanager_idlevel
1Bossnull1
2Alice12
3Bob23
Explanation:

The query retrieves the requested records satisfying all problem requirements.

SQL Editor
Loading Editor...
Query Results

Run a query to see results here.