HomeInterview QuestionsExplain Q3, the recursive CTE that walks the org h…

Explain Q3, the recursive CTE that walks the org hierarchy and sums salaries.

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

💡 Model Answer

Q3 uses a recursive CTE to traverse an organizational hierarchy. The anchor part selects the top‑level employee(s) (mgr_id IS NULL) and assigns them a starting level of 1. The recursive part joins each employee to the CTE on the manager relationship, adding one level per depth. This produces a flat table where each row represents an employee and the depth of their position. The final SELECT then groups by emp_name and sums the salary column from this hierarchy. Because the hierarchy contains every employee under a manager, the SUM aggregates the manager’s own salary plus all salaries of subordinates. The HAVING clause filters out managers whose total salary is 100,000 or less. The query is a classic example of using a recursive CTE to compute cumulative values over a tree structure.

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