HomeInterview QuestionsExplain the workflow of the recursive CTE query th…

Explain the workflow of the recursive CTE query that calculates total salary per employee hierarchy.

🟡 Medium Conceptual Mid level
1Times asked
Sep 2026Last seen
Sep 2026First seen

💡 Model Answer

The workflow begins by building a hierarchical view of the organization. The anchor query pulls the root employees (those with no manager) and tags them with level 1. The recursive step repeatedly joins the employee table to the growing hierarchy on the manager–employee relationship, adding each subordinate and incrementing the level. This continues until no new rows can be added, resulting in a complete tree where each row knows its depth. Once the CTE is finished, the outer SELECT aggregates salaries for each manager by grouping on emp_name. Because the hierarchy includes all descendants, the SUM includes the manager’s own salary plus all subordinates’ salaries. The HAVING clause then filters out managers whose aggregated salary is below 100,000. The final result shows only those managers whose teams exceed the threshold, along with the total salary cost of their teams.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500