Part 2 SQL query to get organization hierarchy
Link for all dot net and sql server video tutorial playlists / kudvenkat Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspo... Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. / @aarvikitchen5572 To get the best out of this video, the following concepts need to be understood first. These are already discussed in SQL Server Tutorial. 1. Self-Join - • Self join in sql server - Part 14 2. CTE - • CTE in sql server Part 49 3. Recursive CTE - • Recursive CTE in sql server Part 51 Here is the problem definition: 1. Employees table contains the following columns a) EmployeeId, b) EmployeeName c) ManagerId 2. If an EmployeeId is passed, the query should list down the entire organization hierarchy i.e who is the manager of the EmployeeId passed and who is managers manager and so on till full hierarchy is listed. For example, Scenario 1: If we pass David's EmployeeId to the query, then it should display the organization hierarchy starting from David. Scenario 2: If we pass Lara's EmployeeId to the query, then it should display the organization hierarchy starting from Lara. We will be Employees table for this demo. SQL to create and populate Employees table with test data Create table Employees ( EmployeeID int primary key identity, EmployeeName nvarchar(50), ManagerID int foreign key references Employees(EmployeeID) ) GO Insert into Employees values ('John', NULL) Insert into Employees values ('Mark', NULL) Insert into Employees values ('Steve', NULL) Insert into Employees values ('Tom', NULL) Insert into Employees values ('Lara', NULL) Insert into Employees values ('Simon', NULL) Insert into Employees values ('David', NULL) Insert into Employees values ('Ben', NULL) Insert into Employees values ('Stacy', NULL) Insert into Employees values ('Sam', NULL) GO Update Employees Set ManagerID = 8 Where EmployeeName IN ('Mark', 'Steve', 'Lara') Update Employees Set ManagerID = 2 Where EmployeeName IN ('Stacy', 'Simon') Update Employees Set ManagerID = 3 Where EmployeeName IN ('Tom') Update Employees Set ManagerID = 5 Where EmployeeName IN ('John', 'Sam') Update Employees Set ManagerID = 4 Where EmployeeName IN ('David') GO Here is the SQL that does the job Declare @ID int ; Set @ID = 7; WITH EmployeeCTE AS ( Select EmployeeId, EmployeeName, ManagerID From Employees Where EmployeeId = @ID UNION ALL Select Employees.EmployeeId , Employees.EmployeeName, Employees.ManagerID From Employees JOIN EmployeeCTE ON Employees.EmployeeId = EmployeeCTE.ManagerID ) Select E1.EmployeeName, ISNULL(E2.EmployeeName, 'No Boss') as ManagerName From EmployeeCTE E1 LEFT Join EmployeeCTE E2 ON E1.ManagerID = E2.EmployeeId

Part 3 How does a recursive CTE work
![Beginner to T-SQL [Full Course]](https://i.ytimg.com/vi/cACat4KNncg/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLA4o6xA3UzwmxwP9P-enFU9sgxi6Q)
Beginner to T-SQL [Full Course]

Part 1 How to find nth highest salary in sql

PostgreSQL Full course 2026 | Learn PostgreSQL in One Video

Trump Preps for 80th Birthday, Threatens to Hit Iran, Knicks Historic Win & Elon Musk Trillionaire!?

Learn sql for beginners

Top SQL Queries for Interviews Questions and Answers | SQL Training | Intellipaat

Learn SQL Beginner to Advanced in Under 4 Hours

SQL Tutorial - Full Database Course for Beginners

Italy Coffee Shop Ambience - Relaxing Jazz Music & Smooth Bossa Nova for Your Workday

SQL Server Interview Questions and Answers | SQL Interview Questions

Trump Breaks Promises of "No Wars" & Inflation, Fox News Questions Iran Peace Claims: A Closer Look
![SQL Course for Beginners [Full Course]](https://i.ytimg.com/vi/7S_tz1z_5bA/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLCAEolqW9nvnTsvv0q31O_tNsNdIw)
SQL Course for Beginners [Full Course]

Learn Kafka in 2 hours #2023 | Kafka for Beginners | Kafka | Zookeeper | Kafka Manager

(No Ads) Melt Away Tension 🌸 Gentle Piano & Pastel Flowers for Mindfulness

SQL Views (Visually Explained) | 6 Top Use Cases | #SQL Course 29

Group by in sql server - Part 11

Jazz & Work☕Relaxed Mood with Soft Jazz Instrumental Music & Relax Morning Elegant Bossa Nova Coffee

