Recursive CTE for Graph Traversal
📖 Scenario: You are working with a company's organizational chart stored in a database. Each employee has a manager, and you want to find all employees under a specific manager, including indirect reports.
🎯 Goal: Build a recursive SQL query using a Common Table Expression (CTE) to find all employees reporting directly or indirectly to a given manager.
📋 What You'll Learn
Create a table called
employees with columns id, name, and manager_idInsert the given employee data with their managers
Write a recursive CTE named
subordinates to find all employees under a specific managerSelect all employee names and their levels under the manager
💡 Why This Matters
🌍 Real World
Companies often store organizational charts in databases. Recursive queries help find all employees under a manager, useful for reporting and permissions.
💼 Career
Understanding recursive CTEs is important for database developers and analysts working with hierarchical data like org charts, file systems, or category trees.
Progress0 / 4 steps