Recursive CTE Employee Hierarchy Query – Null Sql

Clear professional description of the code solution

C
Christian Geimer
·Oct 11, 2026·
0 0 7
Free
Prompt
522 words

Prompt

You are an expert SQL developer. Write a complete, production‑ready solution that uses a recursive Common Table Expression (CTE) to retrieve an employee hierarchy from a single “Employees” table. Follow the instructions below precisely and return the answer in the exact format requested.


1. Schema description

Assume the following table exists (use ANSI‑SQL syntax, compatible with PostgreSQL, SQL Server, MySQL 8+, and Oracle 12c+):

CREATE TABLE Employees (
    EmployeeID      INT PRIMARY KEY,
    EmployeeName    VARCHAR(100) NOT NULL,
    ManagerID       INT NULL,               -- references Employees.EmployeeID; NULL = top‑level CEO
    Department      VARCHAR(50) NOT NULL,
    Salary          NUMERIC(12,2) NOT NULL,
    HireDate        DATE NOT NULL
);
  • ManagerID may be NULL for the root node(s).
  • The hierarchy can be arbitrarily deep and may contain multiple roots (e.g., multiple CEOs in a holding company).

2. Required output columns

Your query must return one row per employee with the following columns, in this order:

ColumnDescription
EmployeeIDOriginal employee identifier
EmployeeNameEmployee’s name
ManagerIDDirect manager’s ID (NULL for roots)
HierarchyLevel1 for top‑level roots, 2 for their direct reports, etc.
PathA slash‑separated string of employee names from the root to the current employee (e.g., CEO/VP Marketing/Director )
TotalSubordinatesCount of all direct and indirect reports under the employee (0 for leaf nodes)
TotalSalaryUnderSum of salaries of all direct and indirect reports (0 for leaf nodes)
IsManagerBoolean (TRUE/FALSE) indicating whether the employee has at least one direct report

The result set must be ordered by Path (lexicographically) so that the hierarchy appears in a readable tree order.


3. Tasks

  1. Write the recursive CTE that produces the columns above.
  2. Include a brief, inline comment on each major part of the query (CTE definition, anchor member, recursive member, final SELECT).
  3. Provide a second, optional query that shows only the hierarchy for a given @RootEmployeeID (parameterizable). Use a placeholder like :RootEmployeeID for the value.
  4. Explain the logic in plain English (max 200 words) after the code block.
  5. Supply sample data (INSERT statements) that creates at least three levels of hierarchy, includes a second root, and demonstrates employees with and without subordinates. Use at most 12 rows.
  6. List performance recommendations (indexes, statistics, query hints) specific to the recursive CTE, formatted as a bullet list.
  7. State any limitations (e.g., maximum recursion depth) and how to adjust them for major RDBMS.

4. Output format

Your answer must contain exactly the following sections, in this order, each separated by a blank line:

  1. SQL – Recursive CTE (code block, language sql)
  2. SQL – Parameterized version (code block, language sql)
  3. Explanation (plain paragraph)
  4. Sample data (code block, language sql)
  5. Performance tips (markdown bullet list)
  6. Limitations & adjustments (markdown bullet list)

Do not add any extra sections, titles, or commentary outside the structure above. Use the exact headings shown (e.g., “SQL – Recursive CTE”). Ensure all code is syntactically correct for the target platforms mentioned.

Need help?

Connect with verified experts who can help you succeed.

Related Prompts

More prompts in Data & Analytics

View All